public async Task ConnectAsync()

in iothub/device/src/net451/IotHubClientWebSocket.cs [130:218]


        public async Task ConnectAsync(string host, int port, string scheme, X509Certificate2 clientCertificate, CancellationToken cancellationToken)
        {
            if (Logging.IsEnabled)
                Logging.Enter(this, scheme, $"{nameof(IotHubClientWebSocket)}.{nameof(ConnectAsync)}");

            _host = host;
            bool succeeded = false;
            try
            {
                // Connect without proxy
                _tcpClient = new TcpClient();
                await _tcpClient.ConnectAsync(host, port).ConfigureAwait(false);

                if (string.Equals(WebSocketConstants.Scheme, scheme, StringComparison.OrdinalIgnoreCase))
                {
                    // In the real world, web-socket will happen over HTTPS
                    var sslStream = new SslStream(_tcpClient.GetStream(), false, OnRemoteCertificateValidation);
                    var x509CertificateCollection = new X509Certificate2Collection();
                    if (clientCertificate != null)
                    {
                        x509CertificateCollection.Add(clientCertificate);
                    }

                    await sslStream
                        .AuthenticateAsClientAsync(
                            host,
                            x509CertificateCollection,
                            enabledSslProtocols: TlsVersions.Instance.Preferred,
                            checkCertificateRevocation: TlsVersions.Instance.CertificateRevocationCheck)
                        .ConfigureAwait(false);

                    _webSocketStream = sslStream;
                }
                else
                {
                    _webSocketStream = _tcpClient.GetStream();
                }

                string upgradeRequest = BuildUpgradeRequest();
                byte[] upgradeRequestBytes = Encoding.ASCII.GetBytes(upgradeRequest);

                // Send WebSocket Upgrade request
                await _webSocketStream.WriteAsync(upgradeRequestBytes, 0, upgradeRequestBytes.Length, cancellationToken).ConfigureAwait(false);

                // receive WebSocket Upgrade response
                byte[] responseBuffer = new byte[8 * 1024];

                // The response object is not returned to the user so it can be disposed.
                using var upgradeResponse = new HttpResponse(_tcpClient, _webSocketStream, responseBuffer);

                await upgradeResponse.ReadAsync(cancellationToken).ConfigureAwait(false);

                if (upgradeResponse.StatusCode != HttpStatusCode.SwitchingProtocols)
                {
                    // the HTTP response code was not 101
                    if (_tcpClient.Connected)
                    {
                        _webSocketStream.Close();
                        _tcpClient.Close();
                    }

                    throw new IOException(ServerRejectedUpgradeRequest + " " + upgradeResponse);
                }

                if (!VerifyWebSocketUpgradeResponse(upgradeResponse.Headers))
                {
                    if (_tcpClient.Connected)
                    {
                        _webSocketStream.Close();
                        _tcpClient.Close();
                    }

                    throw new IOException(UpgradeProtocolNotSupported.FormatInvariant(WebSocketConstants.SubProtocols.Amqpwsb10));
                }

                State = WebSocketState.Open;
                succeeded = true;
            }
            finally
            {
                if (!succeeded)
                {
                    Abort();
                }

                if (Logging.IsEnabled)
                    Logging.Exit(this, scheme, $"{nameof(IotHubClientWebSocket)}.{nameof(ConnectAsync)}");
            }
        }