public HandshakeHandler(
     TcpConnectionService tcpConnectionService,
     WebsocketParserHandler websocketParserHandler,
     Action <ConnectionStatus, Exception?> connectionStatusAction)
 {
     _tcpConnectionService   = tcpConnectionService;
     _websocketParserHandler = websocketParserHandler;
     _connectionStatusAction = connectionStatusAction;
 }
        internal WebsocketConnectionHandler(
            TcpConnectionService tcpConnectionService,
            WebsocketParserHandler websocketParserHandler,
            Action <ConnectionStatus, Exception?> connectionStatusAction,
            Func <Stream, Action <ConnectionStatus, Exception?>, WebsocketSenderHandler> createWebsocketSenderFunc)
        {
            _tcpConnectionService      = tcpConnectionService;
            _websocketParserHandler    = websocketParserHandler;
            _connectionStatusAction    = connectionStatusAction;
            _createWebsocketSenderFunc = createWebsocketSenderFunc;

            _clientPingDisposable = default;
        }
        internal async Task ConnectToWebSocketServer(
            WebsocketParserHandler websocketParserHandler,
            WebsocketSenderHandler websocketSenderHandler,
            Uri uri,
            bool secure,
            Stream tcpStream,
            string origin = null,
            IDictionary <string, string> headers = null,
            IEnumerable <string> subprotocols    = null)
        {
            _websocketParserHandler = websocketParserHandler;
            _websocketSenderHandler = websocketSenderHandler;

            TcpStream = tcpStream;

            _observerConnectionStatus.OnNext(ConnectionStatus.HandshakeSendToWebsocketServer);

            await SendConnectHandShakeAsync(uri, secure, origin, headers, subprotocols);

            var waitForHandShakeResult = await _websocketParserHandler.ParserDelegate
                                         .HandshakeParserCompletionObservable
                                         .Timeout(TimeSpan.FromSeconds(30))
                                         .Catch <ParserState, TimeoutException>(tx => Observable.Return(ParserState.HandshakeTimedOut));

            switch (waitForHandShakeResult)
            {
            case ParserState.HandshakeCompletedSuccessfully:
                _observerConnectionStatus.OnNext(ConnectionStatus.HandshakeCompletedSuccessfully);
                break;

            case ParserState.HandshakeFailed:
                throw new WebsocketClientLiteException("Unable to complete handshake");

            case ParserState.HandshakeTimedOut:
                throw new WebsocketClientLiteException("Handshake timed out.");

            default:
                throw new ArgumentOutOfRangeException($"Unknown parser state: {waitForHandShakeResult}");
            }
        }