class Redis

def connect_to(host, port, timeout=nil)

def connect_to(host, port, timeout=nil)
  # We support connect() timeout only if system_timer is availabe
  # or if we are running against Ruby >= 1.9
  # Timeout reading from the socket instead will be supported anyway.
  if @timeout != 0 and RedisTimer
    begin
      sock = TCPSocket.new(host, port)
    rescue Timeout::Error
      @sock = nil
      raise Timeout::Error, "Timeout connecting to the server"
    end
  else
    sock = TCPSocket.new(host, port)
  end
  sock.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1
  # If the timeout is set we set the low level socket options in order
  # to make sure a blocking read will return after the specified number
  # of seconds. This hack is from memcached ruby client.
  if timeout
    secs   = Integer(timeout)
    usecs  = Integer((timeout - secs) * 1_000_000)
    optval = [secs, usecs].pack("l_2")
    begin
      sock.setsockopt Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, optval
      sock.setsockopt Socket::SOL_SOCKET, Socket::SO_SNDTIMEO, optval
    rescue Exception => ex
      # Solaris, for one, does not like/support socket timeouts.
      @logger.info "Unable to use raw socket timeouts: #{ex.class.name}: #{ex.message}" if @logger
    end
  end
  sock
end