private void ListenWorkerThread()
        {
            ArraySegment <byte> receiveBuffer = new ArraySegment <byte>(new byte[receiveChunkSize]);
            bool                   wasClosed  = true;
            TimeSpan               keepAlive  = webSocket.Options.KeepAliveInterval;
            MemoryStream           stream     = new MemoryStream();
            WebSocketReceiveResult result;

            while (!disposed)
            {
                try
                {
                    if (wasClosed)
                    {
                        // re-open the socket
                        wasClosed = false;
                        webSocket = new ClientWebSocket();
                        webSocket.ConnectAsync(uri, CancellationToken.None).GetAwaiter().GetResult();
                        QueueActionWithNoExceptions(onConnected);
                    }

                    while (webSocket.State == WebSocketState.Open)
                    {
                        do
                        {
                            result = webSocket.ReceiveAsync(receiveBuffer, cancellationToken).GetAwaiter().GetResult();
                            if (result.MessageType == WebSocketMessageType.Close)
                            {
                                webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).GetAwaiter().GetResult();
                                QueueAction(onDisconnected);
                            }
                            else
                            {
                                stream.Write(receiveBuffer.Array, 0, result.Count);
                            }
                        }while (!result.EndOfMessage);
                        if (stream.Length != 0)
                        {
                            // make a copy of the bytes, the memory stream will be re-used and could potentially corrupt in multi-threaded environments
                            byte[] bytesCopy = new byte[stream.Length];
                            Array.Copy(stream.GetBuffer(), bytesCopy, stream.Length);
                            stream.SetLength(0);
                            messageQueue.Add(bytesCopy);
                        }
                    }
                }
                catch
                {
                    QueueAction(onDisconnected);
                    if (!disposed)
                    {
                        // wait one half second before attempting reconnect
                        Task.Delay(500).ConfigureAwait(false).GetAwaiter().GetResult();
                    }
                }
                finally
                {
                    wasClosed = true;
                    try
                    {
                        webSocket.Dispose();
                    }
                    catch
                    {
                    }
                }
            }
        }
        private void ListenWorkerThread()
        {
            ArraySegment <byte> receiveBuffer = new ArraySegment <byte>(new byte[receiveChunkSize]);
            bool                   wasClosed  = true;
            TimeSpan               keepAlive  = _ws.Options.KeepAliveInterval;
            MemoryStream           stream     = new MemoryStream();
            WebSocketReceiveResult result;

            while (!_disposed)
            {
                try
                {
                    if (wasClosed)
                    {
                        // re-open the socket
                        wasClosed = false;
                        _ws       = new ClientWebSocket();
                        _ws.ConnectAsync(_uri, CancellationToken.None).GetAwaiter().GetResult();
                        QueueActionWithNoExceptions(_onConnected);
                    }

                    while (_ws.State == WebSocketState.Open)
                    {
                        do
                        {
                            result = _ws.ReceiveAsync(receiveBuffer, _cancellationToken).GetAwaiter().GetResult();
                            if (result.MessageType == WebSocketMessageType.Close)
                            {
                                _ws.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None).GetAwaiter().GetResult();
                                QueueAction(_onDisconnected);
                            }
                            else
                            {
                                stream.Write(receiveBuffer.Array, 0, result.Count);
                            }
                        }while (!result.EndOfMessage);
                        if (stream.Length != 0)
                        {
                            string messageString = Encoding.UTF8.GetString(stream.GetBuffer(), 0, (int)stream.Length);
                            stream.SetLength(0);
                            _messageQueue.Add(messageString);
                        }
                    }
                }
                catch
                {
                    QueueAction(_onDisconnected);
                    if (!_disposed)
                    {
                        // wait one half second before attempting reconnect
                        Task.Delay(500).Wait();
                    }
                }
                finally
                {
                    wasClosed = true;
                    try
                    {
                        _ws.Dispose();
                    }
                    catch
                    {
                    }
                }
            }
        }