Index: /branches/eraser6/Manager/RemoteExecutor.cs
===================================================================
--- /branches/eraser6/Manager/RemoteExecutor.cs	(revision 748)
+++ /branches/eraser6/Manager/RemoteExecutor.cs	(revision 749)
@@ -100,16 +100,8 @@
 
 		/// <summary>
-		/// Our pipe instance which handles connections.
-		/// </summary>
-		private NamedPipeServerStream server;
-
-		/// <summary>
 		/// Constructor.
 		/// </summary>
 		public RemoteExecutorServer()
 		{
-			server = new NamedPipeServerStream(ServerName, PipeDirection.InOut, 4,
-				PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
-
 			thread = new Thread(Main);
 			thread.Start();
@@ -131,17 +123,41 @@
 			{
 				//Wait for a connection to be established
-				if (!server.IsConnected)
-				{
-					IAsyncResult asyncWait = server.BeginWaitForConnection(
-						EndWaitForConnection, null);
-					while (!server.IsConnected && !asyncWait.AsyncWaitHandle.WaitOne(15))
-						if (Thread.CurrentThread.ThreadState == ThreadState.AbortRequested)
-							break;
-				}
-
-				//If we still aren't connected that means the connection failed to establish.
-				if (!server.IsConnected)
-					continue;
-
+				NamedPipeServerStream server = new NamedPipeServerStream(ServerName,
+					PipeDirection.InOut, 4, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
+				IAsyncResult asyncWait = server.BeginWaitForConnection(
+					EndWaitForConnection, server);
+
+				while (!server.IsConnected && !asyncWait.AsyncWaitHandle.WaitOne(15))
+					if (Thread.CurrentThread.ThreadState == ThreadState.AbortRequested)
+						break;
+
+				//Execute the handler if the server was connected.
+				if (server.IsConnected)
+					ThreadPool.QueueUserWorkItem(ProcessConnection, server);
+			}
+		}
+
+		/// <summary>
+		/// Waits for a connection from a client.
+		/// </summary>
+		/// <param name="result">The AsyncResult object associated with this asynchronous
+		/// operation.</param>
+		private void EndWaitForConnection(IAsyncResult result)
+		{
+			NamedPipeServerStream server = (NamedPipeServerStream)result.AsyncState;
+			server.WaitForConnection();
+			if (server.IsConnected)
+				server.EndWaitForConnection(result);
+		}
+
+		/// <summary>
+		/// Handles a new connection from the client.
+		/// </summary>
+		/// <param name="param">The connected NamedPipeServerStream instance.</param>
+		private void ProcessConnection(object param)
+		{
+			//Get the Server instance.
+			using (NamedPipeServerStream server = (NamedPipeServerStream)param)
+			{
 				//Read the request into the buffer.
 				RemoteRequest request = null;
@@ -160,5 +176,5 @@
 					//Ignore the request if the client disconnected from us.
 					if (!server.IsConnected)
-						continue;
+						return;
 
 					//Deserialise the header of the request.
@@ -172,5 +188,5 @@
 					{
 						//We got a unserialisation issue but we can't do anything about it.
-						continue;
+						return;
 					}
 				}
@@ -242,9 +258,9 @@
 					// void \+ ref task
 					case RemoteRequest.Function.ADD_TASK:
-					{
-						Task task = (Task)parameter;
-						AddTask(ref task);
-						break;
-					}
+						{
+							Task task = (Task)parameter;
+							AddTask(ref task);
+							break;
+						}
 
 					// bool \+ taskid
@@ -298,20 +314,5 @@
 					server.Write(buffer, 0, sizeof(int));
 				}
-
-				//We are done, disconnect
-				server.Disconnect();
 			}
-		}
-
-		/// <summary>
-		/// Waits for a connection from a client.
-		/// </summary>
-		/// <param name="result">The AsyncResult object associated with this asynchronous
-		/// operation.</param>
-		private void EndWaitForConnection(IAsyncResult result)
-		{
-			server.WaitForConnection();
-			if (server.IsConnected)
-				server.EndWaitForConnection(result);
 		}
 	}
