public void ListenForClients(object serverSocket)
        {
            TcpListener tcpListener = (TcpListener)serverSocket;

            tcpListener.Start();
            PrintReceivedData("Server started Listening...");
            while (started)
            {
                try
                {
                    TcpClient client = tcpListener.AcceptTcpClient();
                    clientTable.Add(clientCount, client);
                    Thread clientListShow = new Thread(AddClientToList);
                    clientListShow.Start(clientCount);
                    LogServerForm.Message($"Client - ClientID_{clientCount} connected!");
                    PrintReceivedData($"Client - ClientID_{clientCount} connected!");
                    Thread clientThread = new Thread(HandleClients);
                    clientThread.Start(clientCount);
                    clientCount++;
                }
                catch (Exception ex)
                {
                    LogServerForm.Error(ex);
                }
            }
        }
 public static void Broadcast(string data)
 {
     try
     {
         byte[] buffer = Encoding.ASCII.GetBytes(data + Environment.NewLine);
         foreach (TcpClient client in clientTable.Values)
         {
             NetworkStream stream = client.GetStream();
             stream.Write(buffer, 0, buffer.Length);
         }
     }
     catch (Exception ex)
     {
         LogServerForm.Error(ex);
     }
 }
        public void HandleClients(object obj)
        {
            try
            {
                int       clientID = (int)obj;
                TcpClient client;
                client = clientTable[clientID];

                while (started)
                {
                    try
                    {
                        NetworkStream stream          = client.GetStream();
                        byte[]        buffer          = new byte[1024];
                        int           bufferNoOfBytes = stream.Read(buffer, 0, buffer.Length);
                        if (bufferNoOfBytes == 0)
                        {
                            break;
                        }
                        string data = Encoding.ASCII.GetString(buffer, 0, bufferNoOfBytes);
                        Broadcast(data);
                        LogServerForm.Message(data);
                        PrintReceivedData(data);
                    }
                    catch (Exception ex)
                    {
                        LogServerForm.Error(ex);
                    }
                }
                LogServerForm.Message($"Client - ClientID_{clientID} disconnected!");
                PrintReceivedData($"Client - Client ID_{clientID} disconnected!");
                clientTable.Remove(clientID);
                ClientAppendToList(clientID.ToString());
                client.GetStream().Close();
                client.Close();
                client.Close();
            }
            catch (Exception ex)
            {
                LogServerForm.Error(ex);
            }
        }