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

                if (asyncMode)
                {
                    Exception eOuter = null;
                    var       evt    = new ManualResetEvent(false);
                    int       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);
                            }
                        }
                        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);
                            }
                        }
                        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);
            }
        }