class IO::Endpoint::Wrapper

def connect(remote_address, local_address: nil, **options)

Options Hash: (**local_address)
  • The (Address) -- local address to bind to before connecting.

Parameters:
  • remote_address (Address) -- The remote address to connect to.
def connect(remote_address, local_address: nil, **options)
	socket = build(remote_address.afamily, remote_address.socktype, remote_address.protocol, **options) do |socket|
		if local_address
			if defined?(IP_BIND_ADDRESS_NO_PORT)
				# Inform the kernel (Linux 4.2+) to not reserve an ephemeral port when using bind(2) with a port number of 0. The port will later be automatically chosen at connect(2) time, in a way that allows sharing a source port as long as the 4-tuple is unique.
				socket.setsockopt(SOL_IP, IP_BIND_ADDRESS_NO_PORT, 1)
			end
			
			socket.bind(local_address.to_sockaddr)
		end
	end
	
	begin
		socket.connect(remote_address.to_sockaddr)
	rescue Exception
		socket.close
		raise
	end
	
	return socket unless block_given?
	
	begin
		yield socket
	ensure
		socket.close
	end
end