public byte[] ReadPacket(Stream stream) { while (!PacketAvailable()) { // We can't extract a complete package yet. Fill the buffers. ReadAndDecompress(stream); } // Read one package var result = _handler.ReadPacket(_memorybuffer); // Is the buffer clear for now? If so, dispose all the contents and start over if (_memorybuffer.Position == _memorybuffer.Length) { _memorybuffer.Position = 0; _memorybuffer.SetLength(0); } return(result); }