public Packet[] ReceivePackets(Stream stream)
        {
            //Read the most recent packet received in the current batch.
            var newLastReceivedFromOther = ReadLong(stream);

            //Get the number of new packets received in this call, this number may be negative, which is fine as the packets will be read and discarded.
            var numPackets2 = newLastReceivedFromOther - lastReceivedFromOther;

            //Read the most recent packet received from the other client.
            var newLastReceivedFromMe = ReadLong(stream);

            var amount = newLastReceivedFromMe - lastReceivedFromMe;
            if (amount > 0)
            {
                lock (lockobj)
                    _queue.RemoveRange(0, (int)amount);
                lastReceivedFromMe = newLastReceivedFromMe;
            }

            //The number of packets received in this batch
            var numPackets = ReadLong(stream);

            //Only return new packets
            Packet[] packets = new Packet[numPackets2 < 0 ? 0 : numPackets2];
            for (var i = 0; i < numPackets; i++)
            {
                //If the packet is new, store it
                if (newLastReceivedFromOther - numPackets + i >= lastReceivedFromOther)
                    packets[newLastReceivedFromOther - numPackets + i - lastReceivedFromOther] = Packet.ReadPacket(stream);
                else
                    //We still have to read it if it's old, but we just don't use it.
                    Packet.ReadPacket(stream);
            }
            lastReceivedFromOther = newLastReceivedFromOther;

            return packets;
        }
 public static void WritePacket(List<byte> stream, Packet packet)
 {
     stream.Add(packet.typeID);
     packet.WritePacketData(stream);
 }
 public void AddPacket(Packet packet)
 {
     lock (lockobj)
         _queue.Add(packet);
     ++id;
 }