/// <summary> /// Stops the server and closes any open connections. /// </summary> public void Stop() { if (this.isStarted) { // Stop accepting connections listenSocket.Close(); // Stop trying to clean up sockets this.socketCleanupTimer.Stop(); // Stop any client connections int connected = 0; while (connectedSockets.Count > 0) { // if this happens, then the socket could not be closed properly for some reason. // we have no choice but to abandon the routine and force the stop anyway. // (hopefully, dangling sockets will get cleaned up later by the GC) if (connectedSockets.Count == connected) { this.connectedSockets.Clear(); this.connectedHandlers.Clear(); break; } connected = connectedSockets.Count; // Call Disconnect on the socket, // which will invoke the DidDisconnect method, // which will remove the socket and handler from the list. // (we have to use some trickery to a single item from the list without knowing the key) AsyncSocket someSocket = connectedSockets[0].Socket; if (someSocket != null) { someSocket.CloseImmediately(); } } if (this.bonjour != null) { this.bonjour.Stop(); } LogInfo("Stopped Growl server"); isStarted = false; } }