/// <summary>
        /// Decodes the bytes in the packet-entites message.
        /// </summary>
        /// <param name="packetEntities">Packet entities.</param>
        /// <param name="reader">Reader.</param>
        /// <param name="parser">Parser.</param>
        public static void Apply(PacketEntities packetEntities, IBitStream reader, DemoParser parser)
        {
            int currentEntity = -1;

            for (int i = 0; i < packetEntities.UpdatedEntries; i++)
            {
                //First read which entity is updated
                currentEntity += 1 + (int)reader.ReadUBitInt();

                //Find out whether we should create, destroy or update it.
                // Leave flag
                if (!reader.ReadBit())
                {
                    // enter flag
                    if (reader.ReadBit())
                    {
                        //create it
                        var e = ReadEnterPVS(reader, currentEntity, parser);

                        parser.RawEntities[currentEntity] = e;

                        e.ApplyUpdate(reader);
                    }
                    else
                    {
                        // preserve / update
                        EntityInfo e = parser.RawEntities[currentEntity];
                        e.ApplyUpdate(reader);
                    }
                }
                else
                {
                    EntityInfo e = parser.RawEntities[currentEntity];
                    e.ServerClass.AnnounceDestroyedEntity(e);

                    // leave / destroy
                    e.Leave();
                    parser.RawEntities[currentEntity] = null;

                    //dunno, but you gotta read this.
                    if (reader.ReadBit())
                    {
                    }
                }
            }
        }
        /// <summary>
        /// Reads an update that occures when a new edict enters the PVS (potentially visible system)
        /// </summary>
        /// <returns>The new Entity.</returns>
        private static EntityInfo ReadEnterPVS(IBitStream reader, int id, DemoParser parser)
        {
            //What kind of entity?
            int serverClassID = (int)reader.ReadInt(parser.SendTableParser.ClassBits);

            //So find the correct server class
            ServerClass entityClass = parser.SendTableParser.ServerClasses[serverClassID];

            reader.ReadInt(10); //Entity serial.
            //Never used anywhere I guess. Every parser just skips this


            EntityInfo newEntity = new EntityInfo(id, entityClass);

            //give people the chance to subscribe to events for this
            newEntity.ServerClass.AnnounceNewEntity(newEntity);

            //And then parse the instancebaseline.
            //basically you could call
            //newEntity.ApplyUpdate(parser.instanceBaseline[entityClass];
            //This code below is just faster, since it only parses stuff once
            //which is faster.

            object[] fastBaseline;
            if (parser.PreprocessedBaselines.TryGetValue(serverClassID, out fastBaseline))
            {
                PropertyEntry.Emit(newEntity, fastBaseline);
            }
            else
            {
                var preprocessedBaseline = new List <object>();
                if (parser.instanceBaseline.ContainsKey(serverClassID))
                {
                    using (var collector = new PropertyCollector(newEntity, preprocessedBaseline))
                        using (var bitStream = BitStreamUtil.Create(parser.instanceBaseline[serverClassID]))
                            newEntity.ApplyUpdate(bitStream);
                }

                parser.PreprocessedBaselines.Add(serverClassID, preprocessedBaseline.ToArray());
            }

            return(newEntity);
        }