def connect2
      address = Socket.getaddrinfo(@host, nil, Socket::AF_INET).first[3]
      socket_address = Socket.pack_sockaddr_in(@port, address)

      socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
      socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)

      expire_time = Time.now + @timeout
      begin
        raise Timeout if Time.now > expire_time
        socket.connect_nonblock(socket_address)
      rescue Errno::EISCONN
        nil #continue
      rescue IO::WaitReadable, IO::WaitWritable, Errno::EADDRNOTAVAIL
        select_timeout = expire_time - Time.now
        select_timeout = 0 if select_timeout < 0
        IO.select([socket], [socket], [socket], select_timeout)
        retry
      end

      original_socket = socket
      if @ssl_context
        socket = OpenSSL::SSL::SSLSocket.new(socket, @ssl_context)
        socket.hostname = @host
        socket.sync_close = true
        begin
          raise Timeout if Time.now > expire_time
          socket.connect_nonblock
        rescue IO::WaitReadable, IO::WaitWritable
          select_timeout = expire_time - Time.now
          select_timeout = 0 if select_timeout < 0
          IO.select([socket.io], [socket.io], [socket.io], select_timeout)
          retry
        end
      end
      return socket, original_socket
    end
def connect2
      address = Socket.getaddrinfo(@host, nil, Socket::AF_INET).first[3]
      socket_address = Socket.pack_sockaddr_in(@port, address)

      socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
      socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)

      expire_time = Time.now + @timeout
      begin
        raise Timeout if Time.now > expire_time
        socket.connect_nonblock(socket_address)
      rescue Errno::EISCONN
        nil #continue
      rescue IO::WaitReadable, IO::WaitWritable, Errno::EADDRNOTAVAIL
        select_timeout = expire_time - Time.now
        select_timeout = 0 if select_timeout < 0
        IO.select([socket], [socket], [socket], select_timeout)
        retry
      end

      original_socket = socket
      if @ssl_context
        socket = OpenSSL::SSL::SSLSocket.new(socket, @ssl_context)
        socket.hostname = @host
        socket.sync_close = true
        begin
          raise Timeout if Time.now > expire_time
          socket.connect_nonblock
        rescue IO::WaitReadable, IO::WaitWritable
          select_timeout = expire_time - Time.now
          select_timeout = 0 if select_timeout < 0
          IO.select([socket.io], [socket.io], [socket.io], select_timeout)
          retry
        end
      end
      return socket, original_socket
    end
private