Which threading method is the most optimal for master server networking?
Following this question, I decided to start with a more concrete approach, rather than being thrown theoretical and conceptual information I cannot integrate: http://stackoverflow.com/questions/5359204/are-socket-async-methods-threaded

The point is to keep fluidity for all the clients, while optimizing the server. This means asynchrony in a way or another, in order not to block the main operation.

Here are some methods I came up with. "Process" is an hypothetical method that handles the data received from the client. Consider this could take between 1-5ms usually, perhaps 500-2000ms for *rare* database calls.

**Using Socket.*Async and looping**

	static void Main()
	{
		Socket listener = new Socket(...);
		listener.Bind(new IPEndPoint(IPAddress.Any, 555));

		List<Socket> clients = new List<Socket>();

		SocketAsyncEventArgs e = new SocketAsyncEventArgs();

		while (true)
		{
			if (listener.AcceptAsync(e))
			{
				clients.Add(e.AcceptSocket);
			}

			foreach (Socket client in clients)
			{
				if (client.ReceiveAsync(e))
				{
					Process(e.Buffer);
				}
			}
		}
	}

Pros:

 - One thread only!
 - Rather simple to manage.

Cons:

 - While(true): Too CPU intensive?
 - Since all ops are following each other, they slow down all clients connected.

I assume this is somehow a good start, perhaps the best of my solutions. If I could mix in a thread pool, split the accept, the receive, and the process in different threads, we might be going somewhere.

**Using Socket.Begin*/End* and ManualResetEvent**

	static class Server
	{
		static Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
		static ManualResetEvent acceptDone = new ManualResetEvent(false);

		static void Main()
		{
			listener.Bind(new IPEndPoint(IPAddress.Any, 555));

			while (true)
			{
				acceptDone.Reset();

				listener.BeginAccept(OnAccept, null);

				acceptDone.WaitOne();
			}
		}

		private static void OnAccept(IAsyncResult ar)
		{
			acceptDone.Set();

			new Receiver(listener.EndAccept(ar));
		}
	}

	class Receiver
	{
		Socket socket;
		byte[] buffer = new byte[1024];
		static ManualResetEvent receiveDone = new ManualResetEvent(false);

		public Receiver(Socket socket)
		{
			this.socket = socket;

			new Thread
			(
				delegate()
				{
					while (true)
					{
						receiveDone.Reset();

						socket.BeginReceive(buffer, 0, 1024, SocketFlags.None, OnReceive, null);

						receiveDone.WaitOne();
					}
				}
			).Start();
		}

		private void OnReceive(IAsyncResult ar)
		{
			receiveDone.Set();

			int received = socket.EndReceive(ar);
			byte[] toProcess = new byte[received];
			Buffer.BlockCopy(buffer, 0, toProcess, 0, received);
			Process(toProcess);
		}
	}

Pros:

 - Fully asynchronous, clients never slowed down by other operations.
 - Using ManualResetEvents allows to stop the server nicely.

Cons:

 - Way too many threads, 1 per client!
 - Wasted processing time since all threads are blocked with reset events?

And finally a way of simplifying this solution, without manual reset events.

**Using Blocking Calls and manual threads**

	static class Server
	{
		static Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

		static void Main()
		{
			listener.Bind(new IPEndPoint(IPAddress.Any, 555));

			while (true)
			{
				new Receiver(listener.Accept());
			}
		}
	}

	class Receiver
	{
		Socket socket;

		public Receiver(Socket socket)
		{
			this.socket = socket;

			new Thread
			(
				delegate()
				{
					while (true)
					{
						byte[] buffer = new byte[1024];
						int received = socket.Receive(buffer);
						byte[] toProcess = new byte[received];
						Buffer.BlockCopy(buffer, 0, toProcess, 0, received);
						Process(toProcess);
					}
				}
			).Start();
		}
	}


**Using a Thread Pool**

I actually have no idea how to use one, could someone give me an example for it?

**Suggestions**

Probably that the solution isn't any of the ones in this post. How would you handle it?

As you can see, I used .*Async methods, Begin*/End* methods, and blocking methods, but all have relatively major cons. 

Thanks in advance :) I can't wait to see S/O's code examples. 
Which threading method is the most optimal for master server networking?
===

Following this question, I decided to start with a more concrete approach, rather than being thrown theoretical and conceptual information I cannot integrate: http://stackoverflow.com/questions/5359204/are-socket-async-methods-threaded

The point is to keep fluidity for all the clients, while optimizing the server. This means asynchrony in a way or another, in order not to block the main operation.

Here are some methods I came up with. "Process" is an hypothetical method that handles the data received from the client. Consider this could take between 1-5ms usually, perhaps 500-2000ms for *rare* database calls.

**Using Socket.*Async and looping**

	static void Main()
	{
		Socket listener = new Socket(...);
		listener.Bind(new IPEndPoint(IPAddress.Any, 555));

		List<Socket> clients = new List<Socket>();

		SocketAsyncEventArgs e = new SocketAsyncEventArgs();

		while (true)
		{
			if (listener.AcceptAsync(e))
			{
				clients.Add(e.AcceptSocket);
			}

			foreach (Socket client in clients)
			{
				if (client.ReceiveAsync(e))
				{
					Process(e.Buffer);
				}
			}
		}
	}

Pros:

 - One thread only!
 - Rather simple to manage.

Cons:

 - While(true): Too CPU intensive?
 - Since all ops are following each other, they slow down all clients connected.

I assume this is somehow a good start, perhaps the best of my solutions. If I could mix in a thread pool, split the accept, the receive, and the process in different threads, we might be going somewhere.

**Using Socket.Begin*/End* and ManualResetEvent**

	static class Server
	{
		static Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
		static ManualResetEvent acceptDone = new ManualResetEvent(false);

		static void Main()
		{
			listener.Bind(new IPEndPoint(IPAddress.Any, 555));

			while (true)
			{
				acceptDone.Reset();

				listener.BeginAccept(OnAccept, null);

				acceptDone.WaitOne();
			}
		}

		private static void OnAccept(IAsyncResult ar)
		{
			acceptDone.Set();

			new Receiver(listener.EndAccept(ar));
		}
	}

	class Receiver
	{
		Socket socket;
		byte[] buffer = new byte[1024];
		static ManualResetEvent receiveDone = new ManualResetEvent(false);

		public Receiver(Socket socket)
		{
			this.socket = socket;

			new Thread
			(
				delegate()
				{
					while (true)
					{
						receiveDone.Reset();

						socket.BeginReceive(buffer, 0, 1024, SocketFlags.None, OnReceive, null);

						receiveDone.WaitOne();
					}
				}
			).Start();
		}

		private void OnReceive(IAsyncResult ar)
		{
			receiveDone.Set();

			int received = socket.EndReceive(ar);
			byte[] toProcess = new byte[received];
			Buffer.BlockCopy(buffer, 0, toProcess, 0, received);
			Process(toProcess);
		}
	}

Pros:

 - Fully asynchronous, clients never slowed down by other operations.
 - Using ManualResetEvents allows to stop the server nicely.

Cons:

 - Way too many threads, 1 per client!
 - Wasted processing time since all threads are blocked with reset events?

And finally a way of simplifying this solution, without manual reset events.

**Using Blocking Calls and manual threads**

	static class Server
	{
		static Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

		static void Main()
		{
			listener.Bind(new IPEndPoint(IPAddress.Any, 555));

			while (true)
			{
				new Receiver(listener.Accept());
			}
		}
	}

	class Receiver
	{
		Socket socket;

		public Receiver(Socket socket)
		{
			this.socket = socket;

			new Thread
			(
				delegate()
				{
					while (true)
					{
						byte[] buffer = new byte[1024];
						int received = socket.Receive(buffer);
						byte[] toProcess = new byte[received];
						Buffer.BlockCopy(buffer, 0, toProcess, 0, received);
						Process(toProcess);
					}
				}
			).Start();
		}
	}


**Using a Thread Pool**

I actually have no idea how to use one, could someone give me an example for it?

**Suggestions**

Probably that the solution isn't any of the ones in this post. How would you handle it?

As you can see, I used .*Async methods, Begin*/End* methods, and blocking methods, but all have relatively major cons. 

Thanks in advance :) I can't wait to see S/O's code examples. 