public void Connect(string url)
        {
            DisposeCurrentConnection();

            if (IsConnected)
            {
                Close();
            }

            var ws = new WebSocket(url);
            ws = new WebSocket(url);
            ws.OnOpen += OnOpen;
            ws.OnMessage += OnMessage;
            ws.OnError += OnError;
            ws.OnClose += OnClose;
            _isWsConnected = false;

            _socketWorker = new WebSocketWorker(ws, RunSocketThread);
            _socketWorker.Start();

            _pingWorker = new WebSocketWorker(ws, RunPingThread);
            _pingWorker.Start();
        
            _ws = ws;
        }
        public void Connect(string url)
        {
            DisposeCurrentConnection();

            if (IsConnected)
            {
                Close();
            }

            var ws = new WebSocket(url);

            ws             = new WebSocket(url);
            ws.OnOpen     += OnOpen;
            ws.OnMessage  += OnMessage;
            ws.OnError    += OnError;
            ws.OnClose    += OnClose;
            _isWsConnected = false;

            _socketWorker = new WebSocketWorker(ws, RunSocketThread);
            _socketWorker.Start();

            _pingWorker = new WebSocketWorker(ws, RunPingThread);
            _pingWorker.Start();

            _ws = ws;
        }
        private void RunPingThread(WebSocketWorker worker)
        {
            WebSocket webSocket = worker.WebSocket;

            int timeoutMilis  = Mathf.FloorToInt(PingTimeout * 1000);
            int intervalMilis = Mathf.FloorToInt(PingInterval * 1000);

            DateTime pingStart;

            while (worker.IsRunning)
            {
                if (!webSocket.IsConnected)
                {
                    Thread.Sleep(ReconnectDelay);
                }
                else
                {
                    // TODO handle pong
                    EmitPacket(new Packet(EnginePacketType.PING));
                    pingStart = DateTime.Now;

                    //while (webSocket.IsConnected && thPinging && (DateTime.Now.Subtract(pingStart).TotalSeconds < timeoutMilis))
                    //{
                    //    Thread.Sleep(200);
                    //}

                    //if (!thPong)
                    //{
                    //    webSocket.Close();
                    //}

                    Thread.Sleep(intervalMilis);
                }
            }
        }
        private void StopWorkers()
        {
            if (_pingWorker != null)
            {
                _pingWorker.Stop();
                _pingWorker = null;
            }

            if (_socketWorker != null)
            {
                _socketWorker.Stop();
                _socketWorker = null;
            }
        }
        private void RunSocketThread(WebSocketWorker worker)
        {
            WebSocket webSocket = worker.WebSocket;

            while (worker.IsRunning)
            {
                if (webSocket.IsConnected)
                {
                    Thread.Sleep(ReconnectDelay);
                }
                else
                {
                    // This causes auto
                    webSocket.Connect();
                }
            }
            webSocket.Close();
        }
        private void StopWorkers()
        {
            if (_pingWorker != null)
            {
                _pingWorker.Stop();
                _pingWorker = null;
            }

            if (_socketWorker != null)
            {
                _socketWorker.Stop();
                _socketWorker = null;
            }
        }
        private void RunPingThread(WebSocketWorker worker)
        {
            WebSocket webSocket = worker.WebSocket;

            int timeoutMilis = Mathf.FloorToInt(PingTimeout * 1000);
            int intervalMilis = Mathf.FloorToInt(PingInterval * 1000);

            DateTime pingStart;

            while (worker.IsRunning)
            {
                if (!webSocket.IsConnected)
                {
                    Thread.Sleep(ReconnectDelay);
                }
                else
                {
                    // TODO handle pong
                    EmitPacket(new Packet(EnginePacketType.PING));
                    pingStart = DateTime.Now;

                    //while (webSocket.IsConnected && thPinging && (DateTime.Now.Subtract(pingStart).TotalSeconds < timeoutMilis))
                    //{
                    //    Thread.Sleep(200);
                    //}

                    //if (!thPong)
                    //{
                    //    webSocket.Close();
                    //}

                    Thread.Sleep(intervalMilis);
                }
            }
        }
 private void RunSocketThread(WebSocketWorker worker)
 {
     WebSocket webSocket = worker.WebSocket;
     while (worker.IsRunning)
     {
         if (webSocket.IsConnected)
         {
             Thread.Sleep(ReconnectDelay);
         }
         else
         {
             // This causes auto 
             webSocket.Connect();
         }
     }
     webSocket.Close();
 }