I am trying to write a simple TCP server in C#. It needs to be able to receive packets and do work on them. Ideally it will only be receiving packets from a single computer, but I'm making it able to handle multiple packets to account for corporate network scans. The server will not need to manage multiple long-running connections, so I was trying to keep it as simple as possible (the async examples all looked complex with many functions to handle connections, data reads, and disconnects). All packets will be signed and verified by an RSA key, which should take care of the network scans and partial receives. What else can be done to improve the robustness of the following program?using System;namespace Base_TCP_Server{    class Program    {        static int Port = 7110;//code from Bomberman        static int MaxThreads = 2;//MAX number of threads, 2 for testing > threads connections        static int DataReadTimeoutMS = 2000;        static System.Threading.WaitHandle[] WaitHandles;        static System.Net.Sockets.Socket ListenSocket;        struct ThreadParamsStruct        {            public System.Threading.AutoResetEvent ThreadHandle;            public System.Net.Sockets.Socket ClientSocket;            public int ThreadIndex;        }        static void Main(string[] args)        {            WaitHandles = new System.Threading.WaitHandle[MaxThreads];            for (int i = 0; i < MaxThreads; ++i)                WaitHandles[i] = new System.Threading.AutoResetEvent(true);            ListenSocket = new System.Net.Sockets.Socket(System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);            ListenSocket.Bind(new System.Net.IPEndPoint(0, Port));            ListenSocket.Listen(25);            for (;;)            {//accept connections and send socket to other thread                Console.WriteLine(Waiting for a connection);                System.Net.Sockets.Socket sock = ListenSocket.Accept();                Console.WriteLine(Got a connection);                Console.WriteLine(Waiting for idle thread);                int index = System.Threading.WaitHandle.WaitAny(WaitHandles);                Console.WriteLine(Starting new thread to process client);                ThreadParamsStruct p = new ThreadParamsStruct() { ThreadHandle = (System.Threading.AutoResetEvent)WaitHandles[index], ClientSocket = sock, ThreadIndex = index };                System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ProcessSocketConnection), p);            }        }        static void ProcessSocketConnection(Object threadState)        {            ThreadParamsStruct ts = (ThreadParamsStruct)threadState;            Console.WriteLine(string.Format(Thread {0} is processing connection {1}, ts.ThreadIndex, ts.ClientSocket.RemoteEndPoint.ToString()));            byte[] fakeStorage = new byte[1024 * 256];            if (ts.ClientSocket.Poll(DataReadTimeoutMS * 1000, System.Net.Sockets.SelectMode.SelectRead))            {                ts.ClientSocket.Receive(fakeStorage);            }            else            {                Console.WriteLine(Got no data, aborting);                goto cleanup;            }            //do data processing/verification/etc. here            if (!true)//verification goes here            {//may need this for a send that didn't make it all the way??? required for signature verification at least                Console.WriteLine(Data failed verification);                goto cleanup;            }            Console.WriteLine(Doing 5 seconds worth of work on data);            DateTime current = DateTime.Now;            current = current.AddSeconds(5);            while (DateTime.Now < current)                ;            cleanup:            Console.WriteLine(Doing clean up tasks);            ts.ClientSocket.Shutdown(System.Net.Sockets.SocketShutdown.Both);            ts.ClientSocket.Close();            ts.ClientSocket.Dispose();//not sure if dispose calls the above 2 lines            for (int i = 0; i < fakeStorage.Length; ++i)                fakeStorage[i] = 0;            ts.ThreadHandle.Set();        }    }}
Multithreaded C# TCP Socket Server
c#;multithreading;networking;socket;tcp