class IO::Endpoint::Wrapper

def connect(remote_address, local_address: nil, linger: nil, timeout: nil, buffered: false, **options)

@parameter local_address [Address] The local address to bind to before connecting.
@parameter linger [Boolean] Wait for data to be sent before closing the socket.
@parameter remote_address [Address] The remote address to connect to.
socket = Async::IO::Socket.connect(Async::IO::Address.tcp("8.8.8.8", 53))
@example
Establish a connection to a given `remote_address`.
def connect(remote_address, local_address: nil, linger: nil, timeout: nil, buffered: false, **options)
	socket = nil
	
	begin
		socket = ::Socket.new(remote_address.afamily, remote_address.socktype, remote_address.protocol)
		
		if linger
			socket.setsockopt(SOL_SOCKET, SO_LINGER, 1)
		end
		
		if buffered == false
			set_buffered(socket, buffered)
		end
		
		if timeout
			set_timeout(socket, timeout)
		end
		
		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
	rescue
		socket&.close
		raise
	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