public void Close() { if (clientState != ClientTCPState.Sleep) { if (clientState == ClientTCPState.Entrance) { clientState = ClientTCPState.Sleep; try { socket.Dispose(); } catch (Exception ex) { Global.serverForm.Debug(ex + ""); } foreach (ClientTCP friend in friends) { SendDataTCP.SendFriendLeave(this, friend); friend.friends.Remove(this); } Global.clientList.Remove(this); } else if (clientState == ClientTCPState.MainLobby) { clientState = ClientTCPState.Sleep; try { socket.Dispose(); } catch (Exception ex) { Global.serverForm.Debug(ex + ""); } foreach (ClientTCP friend in friends) { SendDataTCP.SendFriendLeave(this, friend); friend.friends.Remove(this); } Global.clientList.Remove(this); SendDataTCP.SendGlChatMsg("Server", $"Player { nickname } disconnected."); } else if (clientState == ClientTCPState.GameRoom) { clientState = ClientTCPState.Sleep; if (playerState == NetPlayerState.SearchingForMatch) { room.DeletePlayer(this); } else if (playerState == NetPlayerState.Playing) { room.AbortGameSession(this); } else if (playerState == NetPlayerState.EndPlaying) { room.DeletePlayer(this); } foreach (ClientTCP friend in friends) { SendDataTCP.SendFriendLeave(this, friend); friend.friends.Remove(this); } Global.serverForm.Debug($"GamePlayer {nickname} lost connection"); Global.clientList.Remove(this); } Global.serverForm.RemoveClient(this); } }