public Microsoft.ServiceBus.Channels.IConnection Connect(Uri uri, TimeSpan timeout)
        {
            EventTraceActivity eventTraceActivity = new EventTraceActivity();
            TimeoutHelper      timeoutHelper      = new TimeoutHelper(timeout);
            WebStream          webStream          = (new WebStream(uri, "connection", this.useHttpsMode, eventTraceActivity, uri)).Open();

            Microsoft.ServiceBus.Channels.IConnection webStreamConnection = new WebStreamConnection(uri, this.bufferSize, eventTraceActivity, webStream, uri);
            webStreamConnection = SecureSocketUtil.InitiateSecureClientUpgradeIfNeeded(webStreamConnection, null, this.socketSecurityRole, uri.Host, timeoutHelper.RemainingTime());
            SocketMessageHelper socketMessageHelper = new SocketMessageHelper();
            Message             message             = Message.CreateMessage(socketMessageHelper.MessageVersion, "RelayedConnect", new ConnectMessage(uri));

            TrackingIdHeader.TryAddOrUpdate(message.Headers, eventTraceActivity.ActivityId.ToString());
            if (this.tokenProvider != null)
            {
                string        absoluteUri = RelayedHttpUtility.ConvertToHttpUri(uri).AbsoluteUri;
                SecurityToken token       = this.tokenProvider.GetToken(absoluteUri, "Send", false, timeoutHelper.RemainingTime());
                message.Headers.Add(new RelayTokenHeader(token));
            }
            socketMessageHelper.SendMessage(webStreamConnection, message, timeoutHelper.RemainingTime());
            Message message1 = socketMessageHelper.ReceiveMessage(webStreamConnection, timeoutHelper.RemainingTime());

            if (message1.IsFault)
            {
                throw ErrorUtility.ConvertToError(MessageFault.CreateFault(message1, 65536));
            }
            return(webStreamConnection);
        }
        private void SendRelayedConnectAndReceiveResponse(ClientWebSocketConnection connection, TimeoutHelper timeoutHelper)
        {
            SocketMessageHelper socketMessageHelper = new SocketMessageHelper();
            Message             message             = Message.CreateMessage(socketMessageHelper.MessageVersion, "RelayedConnect", new ConnectMessage(connection.Uri));

            TrackingIdHeader.TryAddOrUpdate(message.Headers, connection.Activity.ActivityId.ToString());
            if (this.tokenProvider != null)
            {
                string        absoluteUri = RelayedHttpUtility.ConvertToHttpUri(connection.Uri).AbsoluteUri;
                SecurityToken token       = this.tokenProvider.GetToken(absoluteUri, "Send", false, timeoutHelper.RemainingTime());
                message.Headers.Add(new RelayTokenHeader(token));
            }
            socketMessageHelper.SendMessage(connection, message, timeoutHelper.RemainingTime());
            Message message1 = socketMessageHelper.ReceiveMessage(connection, timeoutHelper.RemainingTime());

            if (message1.IsFault)
            {
                throw ErrorUtility.ConvertToError(MessageFault.CreateFault(message1, 65536));
            }
        }