static public Task <SocketMessage> AsyncConnect(string host, int port, SocketProtocolType protocolType) { var sessionid = MakeSessionID(); var task = new TaskCompletionSource <SocketMessage>(); tasks.Add(sessionid, task); socket.AsyncConnect(host, port, protocolType, sessionid); return(task.Task); }
public async Task ConnectAsync(string host, int port, bool ssl) { Console.WriteLine(host); var address = (await Dns.GetHostAddressesAsync(host))[0]; var endPoint = new IPEndPoint(address, port); Console.WriteLine(endPoint); _socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); await _socket.AsyncConnect(endPoint); _rawStream = new NetworkStream(_socket, false); if (ssl) { _sslStream = new SslStream(_rawStream); await _sslStream.AuthenticateAsClientAsync(host, null, SslProtocols.Tls12, false); _stream = _sslStream; } else { _stream = _rawStream; } }