public override Int32 Read(Byte[] buf, Int32 off, Int32 len)
            {
                if (stream == null)
                {
                    throw new TTransportException(TTransportException.ExceptionType.NotOpen);
                }

                if (asyncMode)
                {
                    Exception eOuter = null;
                    var       evt    = new ManualResetEvent(false);
                    var       retval = 0;

                    stream.BeginRead(buf, off, len, asyncResult =>
                    {
                        try
                        {
                            if (stream != null)
                            {
                                retval = stream.EndRead(asyncResult);
                            }
                            else
                            {
                                eOuter = new TTransportException(TTransportException.ExceptionType.Interrupted);
                            }
                        }
                        catch (Exception e)
                        {
                            if (stream != null)
                            {
                                eOuter = e;
                            }
                            else
                            {
                                eOuter = new TTransportException(TTransportException.ExceptionType.Interrupted, e.Message, e);
                            }
                        }
                        evt.Set();
                    }, null);

                    evt.WaitOne();

                    if (eOuter != null)
                    {
                        throw eOuter; // rethrow exception
                    }
                    else
                    {
                        return(retval);
                    }
                }
                else
                {
                    return(stream.Read(buf, off, len));
                }
            }
        protected override TTransport AcceptImpl()
        {
            try
            {
                EnsurePipeInstance();

                if (asyncMode)
                {
                    var       evt    = new ManualResetEvent(false);
                    Exception eOuter = null;

                    stream.BeginWaitForConnection(asyncResult =>
                    {
                        try
                        {
                            if (stream != null)
                            {
                                stream.EndWaitForConnection(asyncResult);
                            }
                            else
                            {
                                eOuter = new TTransportException(TTransportException.ExceptionType.Interrupted);
                            }
                        }
                        catch (Exception e)
                        {
                            if (stream != null)
                            {
                                eOuter = e;
                            }
                            else
                            {
                                eOuter = new TTransportException(TTransportException.ExceptionType.Interrupted, e.Message, e);
                            }
                        }
                        evt.Set();
                    }, null);

                    evt.WaitOne();

                    if (eOuter != null)
                    {
                        throw eOuter; // rethrow exception
                    }
                }
                else
                {
                    stream.WaitForConnection();
                }

                var trans = new ServerTransport(stream, asyncMode);
                stream = null;  // pass ownership to ServerTransport
                return(trans);
            }
            catch (TTransportException)
            {
                Close();
                throw;
            }
            catch (Exception e)
            {
                Close();
                throw new TTransportException(TTransportException.ExceptionType.NotOpen, e.Message, e);
            }
        }
            public override void Write(Byte[] buf, Int32 off, Int32 len)
            {
                if (stream == null)
                {
                    throw new TTransportException(TTransportException.ExceptionType.NotOpen);
                }

                // if necessary, send the data in chunks
                // there's a system limit around 0x10000 bytes that we hit otherwise
                // MSDN: "Pipe write operations across a network are limited to 65,535 bytes per write. For more information regarding pipes, see the Remarks section."
                var nBytes = Math.Min(len, 15 * 4096);  // 16 would exceed the limit

                while (nBytes > 0)
                {
                    if (asyncMode)
                    {
                        Exception eOuter = null;
                        var       evt    = new ManualResetEvent(false);

                        stream.BeginWrite(buf, off, nBytes, asyncResult =>
                        {
                            try
                            {
                                if (stream != null)
                                {
                                    stream.EndWrite(asyncResult);
                                }
                                else
                                {
                                    eOuter = new TTransportException(TTransportException.ExceptionType.Interrupted);
                                }
                            }
                            catch (Exception e)
                            {
                                if (stream != null)
                                {
                                    eOuter = e;
                                }
                                else
                                {
                                    eOuter = new TTransportException(TTransportException.ExceptionType.Interrupted, e.Message, e);
                                }
                            }
                            evt.Set();
                        }, null);

                        evt.WaitOne();

                        if (eOuter != null)
                        {
                            throw eOuter; // rethrow exception
                        }
                    }
                    else
                    {
                        stream.Write(buf, off, nBytes);
                    }

                    off   += nBytes;
                    len   -= nBytes;
                    nBytes = Math.Min(len, nBytes);
                }
            }