public async Task SendToRoom(string message, int id) { Console.WriteLine(id); var exists = rooms.TryGetValue(id, out List <string> room); var invalidSockets = new List <string>(); Console.WriteLine(message); if (exists) { foreach (var sockId in room) { Console.WriteLine(sockId); var socket = WebSocketConnectionManager.GetSocketById(sockId); if (socket == null) { invalidSockets.Add(sockId); continue; } try { await SendMessageAsync(sockId, message); } catch (WebSocketException e) { invalidSockets.Add(sockId); await WebSocketConnectionManager.RemoveSocketAsync(sockId); } } room.RemoveAll(s => invalidSockets.Any(t => t == s)); } }