Packet buffer

Split PacketBuffer into a separate class.
Fix PacketBuffer.pop OutOfRange exceptions.