public void Write(byte[] buffer, int offset, int count)
 {
     var state = new StateObject();
     lock (_streamLock)
     {
         _stream.BeginWrite(buffer, offset, count, WriteCallback, state);
     }
     state.Done.WaitOne();
 }
 public int Read(byte[] buffer, int offset, int count)
 {
     var state = new StateObject();
     lock (_streamLock)
     {
         _stream.BeginRead(buffer, offset, count, ReadCallback, state);
     }
     state.Done.WaitOne();
     return state.BytesRead;
 }
        public int ReadByte()
        {
            byte[] buffer = new byte[1];

            var state = new StateObject();
            lock (_streamLock)
            {
                _stream.BeginRead(buffer, 0, 1, ReadCallback, state);
            }
            state.Done.WaitOne();
            return buffer[0];
        }