void RunTcp()
        {
            for (;;)
            {
                try
                {
                    IAsyncResult TCPResult = Listener.BeginAcceptTcpClient(null, null);

                    int WaitResult = WaitHandle.WaitAny(new WaitHandle[] { ShutdownEvent, TCPResult.AsyncWaitHandle });

                    // Shutting down
                    if (WaitResult == 0)
                    {
                        break;
                    }

                    try
                    {
                        TcpClient Client = Listener.EndAcceptTcpClient(TCPResult);

                        Log.WriteLine("Accepted connection from {0}", Client.Client.RemoteEndPoint);

                        NetworkStream Stream = Client.GetStream();

                        AutomationRequestInput Input = AutomationRequestInput.Read(Stream);
                        Log.WriteLine("Received input: {0} (+{1} bytes)", Input.Type, Input.Data.Length);

                        AutomationRequestOutput Output;
                        using (AutomationRequest Request = new AutomationRequest(Input))
                        {
                            PostRequest(Request);
                            Request.Complete.Wait();
                            Output = Request.Output;
                        }

                        Output.Write(Stream);
                        Log.WriteLine("Sent output: {0} (+{1} bytes)", Output.Result, Output.Data.Length);
                    }
                    catch (Exception Ex)
                    {
                        Log.WriteLine("Exception: {0}", Ex.ToString());
                    }
                    finally
                    {
                        TCPResult = null;
                        Log.WriteLine("Closed connection.");
                    }
                }
                catch (Exception Ex)
                {
                    if (!bDisposing)
                    {
                        Log.WriteLine("Exception: {0}", Ex.ToString());
                    }
                }
            }
        }
        public void Run()
        {
            try
            {
                for (;;)
                {
                    TcpClient Client = CurrentClient = Listener.AcceptTcpClient();
                    try
                    {
                        Log.WriteLine("Accepted connection from {0}", Client.Client.RemoteEndPoint);

                        NetworkStream Stream = Client.GetStream();

                        AutomationRequestInput Input = AutomationRequestInput.Read(Stream);
                        Log.WriteLine("Received input: {0} (+{1} bytes)", Input.Type, Input.Data.Length);

                        AutomationRequestOutput Output;
                        using (AutomationRequest Request = new AutomationRequest(Input))
                        {
                            PostRequest(Request);
                            Request.Complete.Wait();
                            Output = Request.Output;
                        }

                        Output.Write(Stream);
                        Log.WriteLine("Sent output: {0} (+{1} bytes)", Output.Result, Output.Data.Length);
                    }
                    catch (Exception Ex)
                    {
                        Log.WriteLine("Exception: {0}", Ex.ToString());
                    }
                    finally
                    {
                        Client.Close();
                        Log.WriteLine("Closed connection.");
                    }
                    CurrentClient = null;
                }
            }
            catch (Exception Ex)
            {
                if (!bDisposing)
                {
                    Log.WriteLine("Exception: {0}", Ex.ToString());
                }
            }
            Log.WriteLine("Closing socket.");
        }