def connect_to(host, port, timeout=nil)
sock = nil
if timeout
MemCacheTimer.timeout(timeout) do
sock = TCPSocket.new(host, port)
end
else
sock = TCPSocket.new(host, port)
end
io = MemCache::BufferedIO.new(sock)
io.read_timeout = timeout
if timeout
secs = Integer(timeout)
usecs = Integer((timeout - secs) * 1_000_000)
optval = [secs, usecs].pack("l_2")
begin
io.setsockopt Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, optval
io.setsockopt Socket::SOL_SOCKET, Socket::SO_SNDTIMEO, optval
rescue Exception => ex
@logger.info "[memcache-client] Unable to use raw socket timeouts: #{ex.class.name}: #{ex.message}" if @logger
end
end
io
end