creates a newSocket connected to the address of
local_addrinfo.
If local_addrinfo is nil, the address of the socket is not bound.
The timeout specify the seconds for timeout. Errno::ETIMEDOUT is
raised when timeout occur.
If a block is given the created socket is yielded for each address.
# File ext/socket/lib/socket.rb, line 47
def connect_internal(local_addrinfo, timeout=nil) # :yields: socket
sock = Socket.new(self.pfamily, self.socktype, self.protocol)
begin
sock.ipv6only! if self.ipv6?
sock.bind local_addrinfo if local_addrinfo
if timeout
begin
sock.connect_nonblock(self)
rescue IO::WaitWritable
if !IO.select(nil, [sock], nil, timeout)
raise Errno::ETIMEDOUT, 'user specified timeout'
end
begin
sock.connect_nonblock(self) # check connection failure
rescue Errno::EISCONN
end
end
else
sock.connect(self)
end
rescue Exception
sock.close
raise
end
if block_given?
begin
yield sock
ensure
sock.close if !sock.closed?
end
else
sock
end
end