public Socket Connect(string hostName, int port) { var endPoint = _endpointResolver.GetEndpoint(hostName, port); var ipAddress = _endpointResolver.GetIp(hostName); var socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); var connectionStateModel = new ConnectionStateHandler { Socket = socket, IsConnectionSuccessful = false }; Console.WriteLine($" >> Connecting [{socket.GetHashCode()}] to {endPoint}"); socket.BeginConnect(endPoint, ConnectCallback, connectionStateModel); _isConnectionMade.WaitOne(); return(connectionStateModel.IsConnectionSuccessful ? socket : null); }