public unsafe override void Encode(BinaryStreamBase stream, HistorianKey prevKey, HistorianValue prevValue, HistorianKey currentKey, HistorianValue currentValue)
        {
            if (currentKey.Timestamp == prevKey.Timestamp &&
                ((currentKey.PointID ^ prevKey.PointID) < 64) &&
                currentKey.EntryNumber == 0 &&
                currentValue.Value1 <= uint.MaxValue && //must be a 32-bit value
                currentValue.Value2 == 0 &&
                currentValue.Value3 == 0)
            {
                if (currentValue.Value1 == 0)
                {
                    stream.Write((byte)((currentKey.PointID ^ prevKey.PointID)));
                }
                else
                {
                    stream.Write((byte)((currentKey.PointID ^ prevKey.PointID) | 64));
                    stream.Write((uint)currentValue.Value1);
                }
                return;
            }

            byte code = 128;

            if (currentKey.Timestamp != prevKey.Timestamp)
            {
                code |= 64;
            }

            if (currentKey.EntryNumber != 0)
            {
                code |= 32;
            }

            if (currentValue.Value1 > uint.MaxValue)
            {
                code |= 16;
            }
            else if (currentValue.Value1 > 0)
            {
                code |= 8;
            }

            if (currentValue.Value2 != 0)
            {
                code |= 4;
            }

            if (currentValue.Value3 > uint.MaxValue)
            {
                code |= 2;
            }
            else if (currentValue.Value3 > 0)
            {
                code |= 1;
            }

            stream.Write(code);

            if (currentKey.Timestamp != prevKey.Timestamp)
            {
                stream.Write7Bit(currentKey.Timestamp ^ prevKey.Timestamp);
            }

            stream.Write7Bit(currentKey.PointID ^ prevKey.PointID);

            if (currentKey.EntryNumber != 0)
            {
                stream.Write7Bit(currentKey.EntryNumber);
            }

            if (currentValue.Value1 > uint.MaxValue)
            {
                stream.Write(currentValue.Value1);
            }
            else if (currentValue.Value1 > 0)
            {
                stream.Write((uint)currentValue.Value1);
            }

            if (currentValue.Value2 != 0)
            {
                stream.Write(currentValue.Value2);
            }

            if (currentValue.Value3 > uint.MaxValue)
            {
                stream.Write(currentValue.Value3);
            }
            else if (currentValue.Value3 > 0)
            {
                stream.Write((uint)currentValue.Value3);
            }
        }