private void HandleClientComm(object client) { // Getting the dataStream TcpClient tcpClient = (TcpClient)client; NetworkStream clientStream = tcpClient.GetStream(); string whoami = StripIP(tcpClient.Client.RemoteEndPoint.ToString()); string given_name = "to-be-determined"; string os_data = "to-be-determined"; // Custom variables byte[] incoming_raw_data = new byte[2048]; int bytesRead; bool disconnect = false; int session = 0; while (!disconnect) { bytesRead = 0; try { bytesRead = clientStream.Read(incoming_raw_data, 0, 2048); } catch { #region Disconnection Routine + return directive DisconnectionRoutine(whoami, ref tcpClient, ref clientStream); return; #endregion } if (bytesRead == 0) { #region Disconnection Routine + return directive DisconnectionRoutine(whoami, ref tcpClient, ref clientStream); return; #endregion } if (CheckValidity(ref incoming_raw_data) == false) { if (session > 0) // We have an amorphous package! We should celebrate it! { administrate activePanel = GetAssosicatedPanel(whoami); if (activePanel != null) // Panel is open { // Passing the communication over to the associated admin panel. activePanel.reportAmorphPack(incoming_raw_data, bytesRead); } continue; } else { #region Disconnection Routine + return directive DisconnectionRoutine(whoami, ref tcpClient, ref clientStream); return; #endregion } } // -- We got a valid raw data - proceeding // -- Be careful to start reading the package from [scheme.Length] // CLIENT IS IDENTIFYING ITSELF if (incoming_raw_data[commonData.scheme.Length] == 10) { // A client can't identify itself twice if (session != 0) { #region Disconnection Routine + return directive DisconnectionRoutine(whoami, ref tcpClient, ref clientStream); return; #endregion } // Interpreting the rest as ASCII string[] nameAndOS = Regex.Split(encoder.GetString(incoming_raw_data, commonData.scheme.Length + 1, bytesRead - commonData.scheme.Length - 1), "<%SEP%>"); if (nameAndOS.Length != 2) { #region Disconnection Routine + return directive DisconnectionRoutine(whoami, ref tcpClient, ref clientStream); return; #endregion } given_name = nameAndOS[0]; os_data = nameAndOS[1]; // Adding the client to the common storage and the list commonData.ns_collection.Add(clientStream); addToList(given_name, os_data, whoami); try { clientStream.Write(commonData.recv_signal, 0, commonData.recv_signal.Length); } catch { } } // GENERAL INFORMATION CHANNEL else if (incoming_raw_data[commonData.scheme.Length] == 20) { // Identification is a must before conducting any further communication if (session == 0) { #region Disconnection Routine + return directive DisconnectionRoutine(whoami, ref tcpClient, ref clientStream); return; #endregion } administrate activePanel = GetAssosicatedPanel(whoami); if (activePanel != null) // Panel is open { // Passing the communication over to the associated admin panel. activePanel.reportIncomingComm(incoming_raw_data, bytesRead); } } else { #region Disconnection Routine + return directive DisconnectionRoutine(whoami, ref tcpClient, ref clientStream); return; #endregion } session++; } }