protected void OnConnection(IAsyncResult iarIn)
        {
            this.connected = true;
            NamedPipeServerStream pipeServerLocal = (NamedPipeServerStream)iarIn.AsyncState;

            pipeServerLocal.EndWaitForConnection(iarIn);

            // Read the message and execute the callback on it.
            byte[] buffer = new byte[BUFFER_SIZE];
            pipeServerLocal.Read(buffer, 0, buffer.Length);
            this.OnRead(Encoding.UTF8.GetString(buffer));

            pipeServerLocal.Close();
            this.connected = false;

            if (!this.active)
            {
                // Don't continue listening.
                return;
            }

            // Create a new recursive wait server.
            pipeServerLocal = CreateServerPipe(
                SimplePipePeer.FormatPipeName(this.pipeName, this.client)
                );
            pipeServerLocal.BeginWaitForConnection(new AsyncCallback(this.OnConnection), pipeServerLocal);
        }
 public void Listen()
 {
     try {
         string pipeName = SimplePipePeer.FormatPipeName(this.pipeName, this.client);
         NamedPipeServerStream pipeServerLocal = CreateServerPipe(pipeName);
         pipeServerLocal.BeginWaitForConnection(new AsyncCallback(this.OnConnection), pipeServerLocal);
     } catch (Exception ex) {
         Debug.WriteLine(ex.Message);
     }
 }
        public void Write(byte[] bufferIn, bool waitSync)
        {
            try {
                NamedPipeClientStream pipeClientLocal = new NamedPipeClientStream(
                    ".",
                    SimplePipePeer.FormatPipeName(this.pipeName, !this.client),
                    PipeDirection.InOut,
                    PipeOptions.Asynchronous
                    );

                // The connect function will indefinitely wait for the pipe to become available.\
                // TODO: Is this thread-safe? I don't think it is.
                int localTimeoutCountdown = 0;
                do
                {
                    try {
                        pipeClientLocal.Connect(1000);
                        this.connected = true;
                    } catch (IOException ex) {
                        localTimeoutCountdown++;
                        if (0 <= this.ConnectionTimeout && this.ConnectionTimeout <= localTimeoutCountdown)
                        {
                            throw new TimeoutException(ex.Message);
                        }
                    }
                } while(!this.connected);

                if (waitSync)
                {
                    pipeClientLocal.Write(bufferIn, 0, bufferIn.Length);
                    pipeClientLocal.Flush();
                    pipeClientLocal.Close();
                    pipeClientLocal.Dispose();
                }
                else
                {
                    // Just kick off the process and finish it below.
                    pipeClientLocal.BeginWrite(bufferIn, 0, bufferIn.Length, new AsyncCallback(this.OnWrite), pipeClientLocal);
                }
            } catch (TimeoutException ex) {
                // TODO: Does this still execute finally{} below?
                throw ex;
            } finally {
                this.connected = false;
            }
        }