public async Task ConnectAsync()

in src/Net/TcpTransport.cs [64:158]


        public async Task ConnectAsync(Address address, ConnectionFactory factory, IHandler handler, CancellationToken cancellationToken)
        {
            IPAddress[] ipAddresses;
            IPAddress ip;
            if (IPAddress.TryParse(address.Host, out ip))
            {
                ipAddresses = new IPAddress[] { ip };
            }
            else
            {
                ipAddresses = await TaskExtensions.GetHostAddressesAsync(address.Host).ConfigureAwait(false);
            }

            // need to handle both IPv4 and IPv6
            Socket socket = null;
            Exception exception = null;
            for (int i = 0; i < ipAddresses.Length; i++)
            {
                if (ipAddresses[i] == null ||
                    (ipAddresses[i].AddressFamily == AddressFamily.InterNetwork && !Socket.OSSupportsIPv4) ||
                    (ipAddresses[i].AddressFamily == AddressFamily.InterNetworkV6 && !Socket.OSSupportsIPv6))
                {
                    continue;
                }

                socket = new Socket(ipAddresses[i].AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                try
                {
                    await socket.ConnectAsync(ipAddresses[i], address.Port, cancellationToken).ConfigureAwait(false);

                    exception = null;
                    break;
                }
                catch (Exception e)
                {
                    exception = e;
                    socket.Dispose();
                    socket = null;
                }
            }

            if (socket == null)
            {
                throw exception ?? new SocketException((int)SocketError.AddressNotAvailable);
            }

            if (handler != null && handler.CanHandle(EventId.SocketConnect))
            {
                handler.Handle(Event.Create(EventId.SocketConnect, connection, null, null, socket));
            }

            if (factory.tcpSettings != null)
            {
                factory.tcpSettings.Configure(socket);
            }

            IAsyncTransport transport;
            if (address.UseSsl)
            {
                RemoteCertificateValidationCallback remoteCertificateValidationCallback = Connection.DisableServerCertValidation ? noneCertValidator : null;
                LocalCertificateSelectionCallback localCertificateSelectionCallback = null;
                var ssl = factory.SslInternal;
                if (ssl != null)
                {
                    remoteCertificateValidationCallback = ssl.RemoteCertificateValidationCallback;
                    localCertificateSelectionCallback = ssl.LocalCertificateSelectionCallback;
                }

                SslStream sslStream = new SslStream(new NetworkStream(socket, true), false, remoteCertificateValidationCallback, localCertificateSelectionCallback);
                if (handler != null && handler.CanHandle(EventId.SslAuthenticate))
                {
                    handler.Handle(Event.Create(EventId.SslAuthenticate, connection, null, null, sslStream));
                }
                else
                {
                    if (ssl == null)
                    {
                        await sslStream.AuthenticateAsClientAsync(address.Host).ConfigureAwait(false);
                    }
                    else
                    {
                        await sslStream.AuthenticateAsClientAsync(address.Host, ssl.ClientCertificates,
                            ssl.Protocols, ssl.CheckCertificateRevocation).ConfigureAwait(false);
                    }
                }

                transport = new SslSocket(this, sslStream);
            }
            else
            {
                transport = new TcpSocket(this, socket);
            }

            this.socketTransport = transport;
        }