public void ObjectAddHandler(Packet packet, Simulator simulator)
        {
            LLVector3 position = null;

            if (WaitingOnUpdate)
            {
                CurrentPrimMutex.WaitOne();

                foreach (Block block in packet.Blocks())
                {
                    foreach (Field field in block.Fields)
                    {
                        if (field.Layout.Name == "RayEnd")
                        {
                            position = (LLVector3)field.Data;
                        }
                    }
                }

                txtLog.AppendText("Received an ObjectAdd, setting CurrentPrim position to " + position.ToString());
                CurrentPrim.Position = position;

                CurrentPrimMutex.ReleaseMutex();
            }
        }
    // LogPacket: dump a packet to the console
    private static void LogPacket(Packet packet, IPEndPoint endPoint, Direction direction)
    {
        if (logGrep != null) {
            bool match = false;
            foreach (Block block in packet.Blocks())
                foreach (Field field in block.Fields) {
                    string value;
                    if (field.Layout.Type == FieldType.Variable)
                        value = DataConvert.toChoppedString(field.Data);
                    else
                        value = field.Data.ToString();
                    if (Regex.Match(packet.Layout.Name + "." + block.Layout.Name + "." + field.Layout.Name + " = " + value, logGrep, RegexOptions.IgnoreCase).Success) {
                        match = true;
                        break;
                    }

                    // try matching variable fields in 0x notation
                    if (field.Layout.Type == FieldType.Variable) {
                        StringWriter sw = new StringWriter();
                        sw.Write("0x");
                        foreach (byte b in (byte[])field.Data)
                            sw.Write("{0:x2}", b);
                        if (Regex.Match(packet.Layout.Name + "." + block.Layout.Name + "." + field.Layout.Name + " = " + sw, logGrep, RegexOptions.IgnoreCase).Success) {
                            match = true;
                            break;
                        }
                    }
                }
            if (!match)
                return;
        }

        Console.WriteLine("{0} {1,21} {2,5} {3}{4}{5}"
                 ,direction == Direction.Incoming ? "<--" : "-->"
                 ,endPoint
                 ,packet.Sequence
                 ,InterpretOptions(packet.Data[0])
                 ,Environment.NewLine
                 ,packet
                 );
    }
        private void DirPeopleHandler(Packet packet, Simulator simulator)
        {
            lstFindMutex.WaitOne();

            foreach (Block block in packet.Blocks())
            {
                if (block.Layout.Name == "QueryReplies")
                {
                    LLUUID id = null;
                    string firstName = "";
                    string lastName = "";
                    bool online = false;

                    foreach (Field field in block.Fields)
                    {
                        if (field.Layout.Name == "AgentID")
                        {
                            id = (LLUUID)field.Data;
                        }
                        else if (field.Layout.Name == "LastName")
                        {
                            lastName = System.Text.Encoding.UTF8.GetString((byte[])field.Data).Replace("\0", "");
                        }
                        else if (field.Layout.Name == "FirstName")
                        {
                            firstName = System.Text.Encoding.UTF8.GetString((byte[])field.Data).Replace("\0", "");
                        }
                        else if (field.Layout.Name == "Online")
                        {
                            online = (bool)field.Data;
                        }
                    }

                    if (id != null)
                    {
                        ListViewItem listItem = new ListViewItem(new string[]
                        { firstName + " " + lastName, (online ? "Yes" : "No"), id.ToString() });
                        lstFind.Items.Add(listItem);
                    }
                }
            }

            lstFindMutex.ReleaseMutex();
        }
        private void BalanceHandler(Packet packet, Simulator simulator)
        {
            if (packet.Layout.Name == "MoneyBalanceReply")
            {
                int balance = 0;
                int squareMetersCredit = 0;
                string description = "";
                LLUUID transactionID = null;
                bool transactionSuccess = false;

                foreach (Block block in packet.Blocks())
                {
                    foreach (Field field in block.Fields)
                    {
                        if (field.Layout.Name == "MoneyBalance")
                        {
                            balance = (int)field.Data;
                        }
                        else if (field.Layout.Name == "SquareMetersCredit")
                        {
                            squareMetersCredit = (int)field.Data;
                        }
                        else if (field.Layout.Name == "Description")
                        {
                            byte[] byteArray = (byte[])field.Data;
                            description = System.Text.Encoding.ASCII.GetString(byteArray).Replace("\0", "");
                        }
                        else if (field.Layout.Name == "TransactionID")
                        {
                            transactionID = (LLUUID)field.Data;
                        }
                        else if (field.Layout.Name == "TransactionSuccess")
                        {
                            transactionSuccess = (bool)field.Data;
                        }
                    }
                }

                lblBalance.Text = balance.ToString();
            }
        }
        private void InstantMessageIncoming(Packet packet, Simulator simulator)
        {
            if (packet.Layout.Name != "ImprovedInstantMessage") return;

            LLUUID FromAgentID   = new LLUUID();
            LLUUID ToAgentID     = new LLUUID();
            uint ParentEstateID  = 0;
            LLUUID RegionID      = new LLUUID();
            LLVector3 Position   = new LLVector3();
            bool Offline         = false;
            byte Dialog          = 0;
            LLUUID ID            = new LLUUID();
            uint Timestamp       = 0;
            DateTime dt          = new DateTime(1970, 1, 1, 0, 0, 0, 0); //The Unix epoch!
            string FromAgentName = string.Empty;
            string Message       = string.Empty;
            string BinaryBucket  = string.Empty;

            ArrayList blocks = packet.Blocks();

            foreach (Block block in blocks)
            {
                foreach (Field field in block.Fields)
                {
                    switch (field.Layout.Name)
                    {
                        case "FromAgentID":
                            FromAgentID = (LLUUID)field.Data;
                            break;

                        case "ToAgentID":
                            ToAgentID = (LLUUID)field.Data;
                            break;

                        case "ParentEstateID":
                            ParentEstateID = (uint)field.Data;
                            break;

                        case "RegionID":
                            RegionID = (LLUUID)field.Data;
                            break;

                        case "Position":
                            Position = (LLVector3)field.Data;
                            break;

                        case "Offline":
                            Offline = ((byte)field.Data == 1 ? true : false);
                            break;

                        case "Dialog":
                            Dialog = (byte)field.Data;
                            break;

                        case "ID":
                            ID = (LLUUID)field.Data;
                            break;

                        case "Timestamp":
                            Timestamp = (uint)field.Data;

                            if (Timestamp == 0) //User is online
                                dt = DateTime.Now;
                            else //User is offline
                                dt = dt.AddSeconds(Timestamp);

                            break;

                        case "FromAgentName":
                            FromAgentName = System.Text.Encoding.UTF8.GetString((byte[])field.Data).Replace("\0", string.Empty);
                            break;

                        case "Message":
                            Message = System.Text.Encoding.UTF8.GetString((byte[])field.Data).Replace("\0", string.Empty);
                            break;

                        case "BinaryBucket":
                            BinaryBucket = System.Text.Encoding.UTF8.GetString((byte[])field.Data).Replace("\0", string.Empty);
                            break;
                    }
                }

                InstantMessageEventArgs eventArgs = new InstantMessageEventArgs(
                    FromAgentID, ToAgentID, ParentEstateID, RegionID,
                    Position, Offline, Dialog, ID,
                    dt, FromAgentName, Message, BinaryBucket);

                if (netcomSync != null)
                {
                    object[] ea = new object[1];
                    ea[0] = eventArgs;
                    netcomSync.Invoke(new OnInstantMessageRaise(OnInstantMessageReceived), ea);
                }
                else
                {
                    OnInstantMessageReceived(eventArgs);
                }
            }
        }
        private void ChatIncoming(Packet packet, Simulator simulator)
        {
            if (packet.Layout.Name != "ChatFromSimulator") return;

            string fromname         = string.Empty; //Name of source.
            LLUUID sourceid         = new LLUUID(); //UUID of source, object/avatar
            LLUUID ownerid          = new LLUUID(); //UUID of owner, if object UUID = owner of object, if avatar UUID = same as source
            SLSourceType sourcetype = SLSourceType.None;
            SLChatType chattype     = SLChatType.Whisper;
            bool audible            = false; //Audible: 1 if audible, 0 if beyond 20m (message is null)
            LLVector3 position      = new LLVector3(); //Region local position of source.
            string message          = string.Empty; //Message from source
            byte command            = 0; //Unused?
            LLUUID commandID        = new LLUUID(); //Unused?

            ArrayList blocks = packet.Blocks();

            foreach (Block block in blocks)
            {
                foreach (Field field in block.Fields)
                {
                    switch (field.Layout.Name)
                    {
                        case "SourceID":
                            sourceid = (LLUUID)field.Data;
                            break;

                        case "OwnerID":
                            ownerid = (LLUUID)field.Data;
                            break;

                        case "FromName":
                            fromname = System.Text.Encoding.UTF8.GetString((byte[])field.Data).Replace("\0", string.Empty);
                            break;

                        case "SourceType":
                            sourcetype = (SLSourceType)(byte)field.Data;
                            break;

                        case "ChatType":
                            chattype = (SLChatType)(byte)field.Data;
                            break;

                        case "Audible":
                            audible = ((byte)field.Data == 1 ? true : false);
                            break;

                        case "Message":
                            message = System.Text.Encoding.UTF8.GetString((byte[])field.Data).Replace("\0", string.Empty);
                            break;

                        case "Position":
                            position = (LLVector3)field.Data;
                            break;

                        case "Command":
                            command = (byte)field.Data;
                            break;

                        case "CommandID":
                            commandID = (LLUUID)field.Data;
                            break;
                    }
                }

                ChatEventArgs eventArgs = new ChatEventArgs(
                    message, chattype,
                    position, sourcetype, sourceid, ownerid,
                    fromname, audible, command, commandID);

                if (netcomSync != null)
                {
                    object[] ea = new object[1];
                    ea[0] = eventArgs;
                    netcomSync.Invoke(new OnChatRaise(OnChatReceived), ea);
                }
                else
                {
                    OnChatReceived(eventArgs);
                }
            }
        }
        public void ObjectUpdateHandler(Packet packet, Simulator simulator)
        {
            uint id = 0;
            LLUUID uuid = null;

            if (WaitingOnUpdate)
            {
                CurrentPrimMutex.WaitOne();

                foreach (Block block in packet.Blocks())
                {
                    foreach (Field field in block.Fields)
                    {
                        if (field.Layout.Name == "ID")
                        {
                            id = (uint)field.Data;
                        }
                        else if (field.Layout.Name == "FullID")
                        {
                            uuid = (LLUUID)field.Data;
                        }
                        else if (field.Layout.Name == "ObjectData")
                        {
                            byte[] byteArray = (byte[])field.Data;
                            LLVector3 position = new LLVector3(byteArray, 0);
                            if (CurrentPrim != null && position != CurrentPrim.Position)
                            {
                                txtLog.AppendText(position.ToString() + " doesn't match CurrentPrim.Position " +
                                    CurrentPrim.Position.ToString() + "\n"/* + ", ignoring"*/);
                                //return;
                            }
                        }
                    }
                }

                CurrentPrim.ID = id;
                CurrentPrim.UUID = uuid;

                WaitingOnUpdate = false;

                CurrentPrimMutex.ReleaseMutex();
            }
        }
        // Unbuild: deconstruct a packet into a Hashtable of blocks suitable for passing to PacketBuilder
        public static Hashtable Unbuild(Packet packet)
        {
            Hashtable blockTable = new Hashtable();
            foreach (Block block in packet.Blocks()) {
                Hashtable fieldTable = new Hashtable();
                foreach (Field field in block.Fields)
                    fieldTable[field.Layout.Name] = field.Data;
                blockTable[fieldTable] = block.Layout.Name;
            }

            return blockTable;
        }