protected override void Execute() { using (_Server = new WebSocketConnection()) { if (_Server.Listen(_Address, _Port)) { RaiseListeningEvent(); while (!_Stop) { try { // Accept an incoming connection if (_Server.CanAccept(1000)) // 1 second { Socket NewSocket = _Server.Accept(); if (NewSocket != null) { lock (_ClientThreadsLock) { WebSocketClientThread ClientThread = new WebSocketClientThread(NewSocket, ++_ClientThreadCounter); ClientThread.FinishEvent += ClientThread_FinishEvent; _ClientThreads.Add(ClientThread); RMLog.Info(_ClientThreads.Count.ToString() + " active connections"); ClientThread.Start(); } } } } catch (Exception ex) { RMLog.Exception(ex, "Unable to accept new websocket connection"); } } // Stop client threads int ClientThreadCount = 0; lock (_ClientThreadsLock) { foreach (var ClientThread in _ClientThreads) { if (ClientThread != null) { ClientThread.Stop(); } } ClientThreadCount = _ClientThreads.Count; } // Wait for client threads while (ClientThreadCount > 0) { lock (_ClientThreadsLock) { ClientThreadCount = _ClientThreads.Count; } Thread.Sleep(100); } } else { RMLog.Error("WebSocket Server Thread: Unable to listen on " + _Address + ":" + _Port); } } }