/// <summary>
        /// Return a decoded capabilities message as a strongly typed object
        /// </summary>
        /// <param name="eventName">A string containing the name of the capabilities message key</param>
        /// <param name="map">An <see cref="OSDMap"/> to decode</param>
        /// <returns>A strongly typed object containing the decoded information from the capabilities message, or null
        /// if no existing Message object exists for the specified event</returns>
        public static IMessage DecodeEvent(string eventName, OSDMap map)
        {
            IMessage message = null;

            switch (eventName)
            {
                case "AgentGroupDataUpdate": message = new AgentGroupDataUpdateMessage(); break;
                case "AvatarGroupsReply": message = new AgentGroupDataUpdateMessage(); break; // OpenSim sends the above with the wrong? key
                case "ParcelProperties": message = new ParcelPropertiesMessage(); break;
                case "ParcelObjectOwnersReply": message = new ParcelObjectOwnersReplyMessage(); break;
                case "TeleportFinish": message = new TeleportFinishMessage(); break;
                case "EnableSimulator": message = new EnableSimulatorMessage(); break;
                case "ParcelPropertiesUpdate": message = new ParcelPropertiesUpdateMessage(); break;
                case "EstablishAgentCommunication": message = new EstablishAgentCommunicationMessage(); break;
                case "ChatterBoxInvitation": message = new ChatterBoxInvitationMessage(); break;
                case "ChatterBoxSessionEventReply": message = new ChatterboxSessionEventReplyMessage(); break;
                case "ChatterBoxSessionStartReply": message = new ChatterBoxSessionStartReplyMessage(); break;
                case "ChatterBoxSessionAgentListUpdates": message = new ChatterBoxSessionAgentListUpdatesMessage(); break;
                case "RequiredVoiceVersion": message = new RequiredVoiceVersionMessage(); break;
                case "MapLayer": message = new MapLayerMessage(); break;
                case "ChatSessionRequest": message = new ChatSessionRequestMessage(); break;
                case "CopyInventoryFromNotecard": message = new CopyInventoryFromNotecardMessage(); break;
                case "ProvisionVoiceAccountRequest": message = new ProvisionVoiceAccountRequestMessage(); break;
                case "Viewerstats": message = new ViewerStatsMessage(); break;
                case "UpdateAgentLanguage": message = new UpdateAgentLanguageMessage(); break;
                case "RemoteParcelRequest": message = new RemoteParcelRequestMessage(); break;
                case "UpdateScriptTask": message = new UpdateScriptTaskMessage(); break;
                case "UpdateScriptAgent": message = new UpdateScriptAgentMessage(); break;
                case "SendPostcard": message = new SendPostcardMessage(); break;
                case "UpdateGestureAgentInventory": message = new UpdateGestureAgentInventoryMessage(); break;
                case "UpdateNotecardAgentInventory": message = new UpdateNotecardAgentInventoryMessage(); break;
                case "LandStatReply": message = new LandStatReplyMessage(); break;
                case "ParcelVoiceInfoRequest": message = new ParcelVoiceInfoRequestMessage(); break;
                case "ViewerStats": message = new ViewerStatsMessage(); break;
                case "EventQueueGet": message = new EventQueueGetMessage(); break;
                case "CrossedRegion": message = new CrossedRegionMessage(); break;
                case "TeleportFailed": message = new TeleportFailedMessage(); break;
                case "PlacesReply": message = new PlacesReplyMessage(); break;
                case "UpdateAgentInformation": message = new UpdateAgentInformationMessage(); break;
                case "DirLandReply": message = new DirLandReplyMessage(); break;
                case "ScriptRunningReply": message = new ScriptRunningReplyMessage(); break;
                case "SearchStatRequest": message = new SearchStatRequestMessage(); break;
                case "AgentDropGroup": message = new AgentDropGroupMessage(); break;
                case "AgentStateUpdate": message = new AgentStateUpdateMessage(); break;
                case "ForceCloseChatterBoxSession": message = new ForceCloseChatterBoxSessionMessage(); break;
                case "UploadBakedTexture": message = new UploadBakedTextureMessage(); break;
                case "RegionInfo": message = new RegionInfoMessage(); break;
                case "ObjectMediaNavigate": message = new ObjectMediaNavigateMessage(); break;
                case "ObjectMedia": message = new ObjectMediaMessage(); break;
                case "AttachmentResources": message = AttachmentResourcesMessage.GetMessageHandler(map); break;
                case "LandResources": message = LandResourcesMessage.GetMessageHandler(map); break;
                case "GetDisplayNames": message = new GetDisplayNamesMessage(); break;
                case "SetDisplayName": message = new SetDisplayNameMessage(); break;
                case "SetDisplayNameReply": message = new SetDisplayNameReplyMessage(); break;
                case "DisplayNameUpdate": message = new DisplayNameUpdateMessage(); break;
                //case "ProductInfoRequest": message = new ProductInfoRequestMessage(); break;
                case "ObjectPhysicsProperties": message = new ObjectPhysicsPropertiesMessage(); break;
                case "BulkUpdateInventory": message = new BulkUpdateInventoryMessage(); break;
                case "RenderMaterials": message = new RenderMaterialsMessage(); break;
                case "GetObjectCost": message = GetObjectCostMessage.GetMessageHandler(map); break;

                // Capabilities TODO:
                // DispatchRegionInfo
                // EstateChangeInfo
                // EventQueueGet
                // FetchInventoryDescendents
                // GroupProposalBallot
                // MapLayerGod
                // NewFileAgentInventory
                // RequestTextureDownload
                // SearchStatTracking
                // SendUserReport
                // SendUserReportWithScreenshot
                // ServerReleaseNotes
                // StartGroupProposal
                // UpdateGestureTaskInventory
                // UpdateNotecardTaskInventory
                // ViewerStartAuction
                // UntrustedSimulatorMessage
            }

            if (message != null)
            {
                try
                {
                    message.Deserialize(map);
                    return message;
                }
                catch (Exception e)
                {
                    Logger.Log("Exception while trying to Deserialize " + eventName + ":" + e.Message + ": " + e.StackTrace, Helpers.LogLevel.Error);
                }

                return null;
            }
            else
            {
                return null;
            }
        }
        public void ReflectionPerformanceDirLandReply()
        {
            DateTime messageTestTime = DateTime.UtcNow;
            for (int x = 0; x < TEST_ITER; x++)
            {
                DirLandReplyMessage s = new DirLandReplyMessage();
                s.AgentID = UUID.Random();
                s.QueryID = UUID.Random();
                s.QueryReplies = new DirLandReplyMessage.QueryReply[2];

                DirLandReplyMessage.QueryReply q1 = new DirLandReplyMessage.QueryReply();
                q1.ActualArea = 1024;
                q1.Auction = true;
                q1.ForSale = true;
                q1.Name = "For Sale Parcel Q1";
                q1.ProductSku = "023";
                q1.SalePrice = 2193;
                q1.ParcelID = UUID.Random();

                s.QueryReplies[0] = q1;

                DirLandReplyMessage.QueryReply q2 = new DirLandReplyMessage.QueryReply();
                q2.ActualArea = 512;
                q2.Auction = true;
                q2.ForSale = true;
                q2.Name = "For Sale Parcel Q2";
                q2.ProductSku = "023";
                q2.SalePrice = 22193;
                q2.ParcelID = UUID.Random();

                s.QueryReplies[1] = q2;

                OSDMap map = s.Serialize();
                DirLandReplyMessage t = new DirLandReplyMessage();

                t.Deserialize(map);
                Assert.AreEqual(s.AgentID, t.AgentID);
                Assert.AreEqual(s.QueryID, t.QueryID);

                for (int i = 0; i < s.QueryReplies.Length; i++)
                {
                    Assert.AreEqual(s.QueryReplies[i].ActualArea, t.QueryReplies[i].ActualArea);
                    Assert.AreEqual(s.QueryReplies[i].Auction, t.QueryReplies[i].Auction);
                    Assert.AreEqual(s.QueryReplies[i].ForSale, t.QueryReplies[i].ForSale);
                    Assert.AreEqual(s.QueryReplies[i].Name, t.QueryReplies[i].Name);
                    Assert.AreEqual(s.QueryReplies[i].ProductSku, t.QueryReplies[i].ProductSku);
                    Assert.AreEqual(s.QueryReplies[i].ParcelID, t.QueryReplies[i].ParcelID);
                    Assert.AreEqual(s.QueryReplies[i].SalePrice, t.QueryReplies[i].SalePrice);
                }
            }

            TimeSpan duration = DateTime.UtcNow - messageTestTime;
            Console.WriteLine("DirLandReplyMessage: OMV Message System Serialization/Deserialization Passes: {0} Total time: {1}", TEST_ITER, duration);

            BinaryFormatter formatter = new BinaryFormatter();
            DateTime xmlTestTime = DateTime.UtcNow;
            for (int x = 0; x < TEST_ITER; x++)
            {
                DirLandReplyMessage s = new DirLandReplyMessage();
                s.AgentID = UUID.Random();
                s.QueryID = UUID.Random();
                s.QueryReplies = new DirLandReplyMessage.QueryReply[2];

                DirLandReplyMessage.QueryReply q1 = new DirLandReplyMessage.QueryReply();
                q1.ActualArea = 1024;
                q1.Auction = true;
                q1.ForSale = true;
                q1.Name = "For Sale Parcel Q1";
                q1.ProductSku = "023";
                q1.SalePrice = 2193;
                q1.ParcelID = UUID.Random();

                s.QueryReplies[0] = q1;

                DirLandReplyMessage.QueryReply q2 = new DirLandReplyMessage.QueryReply();
                q2.ActualArea = 512;
                q2.Auction = true;
                q2.ForSale = true;
                q2.Name = "For Sale Parcel Q2";
                q2.ProductSku = "023";
                q2.SalePrice = 22193;
                q2.ParcelID = UUID.Random();

                s.QueryReplies[1] = q2;

                MemoryStream stream = new MemoryStream();

                formatter.Serialize(stream, s);

                stream.Seek(0, SeekOrigin.Begin);
                DirLandReplyMessage t = (DirLandReplyMessage)formatter.Deserialize(stream);

                Assert.AreEqual(s.AgentID, t.AgentID);
                Assert.AreEqual(s.QueryID, t.QueryID);

                for (int i = 0; i < s.QueryReplies.Length; i++)
                {
                    Assert.AreEqual(s.QueryReplies[i].ActualArea, t.QueryReplies[i].ActualArea);
                    Assert.AreEqual(s.QueryReplies[i].Auction, t.QueryReplies[i].Auction);
                    Assert.AreEqual(s.QueryReplies[i].ForSale, t.QueryReplies[i].ForSale);
                    Assert.AreEqual(s.QueryReplies[i].Name, t.QueryReplies[i].Name);
                    Assert.AreEqual(s.QueryReplies[i].ProductSku, t.QueryReplies[i].ProductSku);
                    Assert.AreEqual(s.QueryReplies[i].ParcelID, t.QueryReplies[i].ParcelID);
                    Assert.AreEqual(s.QueryReplies[i].SalePrice, t.QueryReplies[i].SalePrice);
                }
            }

            TimeSpan durationxml = DateTime.UtcNow - xmlTestTime;
            Console.WriteLine("DirLandReplyMessage: .NET BinarySerialization/Deserialization Passes: {0} Total time: {1}", TEST_ITER, durationxml);
        }
        public void ReflectionPerformanceDirLandReply2()
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(DirLandReplyMessage));

            DirLandReplyMessage s = new DirLandReplyMessage();
            s.AgentID = UUID.Random();
            s.QueryID = UUID.Random();
            s.QueryReplies = new DirLandReplyMessage.QueryReply[2];

            DirLandReplyMessage.QueryReply q1 = new DirLandReplyMessage.QueryReply();
            q1.ActualArea = 1024;
            q1.Auction = true;
            q1.ForSale = true;
            q1.Name = "For Sale Parcel Q1";
            q1.ProductSku = "023";
            q1.SalePrice = 2193;
            q1.ParcelID = UUID.Random();

            s.QueryReplies[0] = q1;

            DirLandReplyMessage.QueryReply q2 = new DirLandReplyMessage.QueryReply();
            q2.ActualArea = 512;
            q2.Auction = true;
            q2.ForSale = true;
            q2.Name = "For Sale Parcel Q2";
            q2.ProductSku = "023";
            q2.SalePrice = 22193;
            q2.ParcelID = UUID.Random();

            s.QueryReplies[1] = q2;

            System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
            timer.Start();
            for (int i = 0; i < TEST_ITER; ++i)
            {
                MemoryStream stream = new MemoryStream();
                OSDMap map = s.Serialize();
                byte[] jsonData = Encoding.UTF8.GetBytes(OSDParser.SerializeJsonString(map));
                stream.Write(jsonData, 0, jsonData.Length);
                stream.Flush();
                stream.Close();
            }

            timer.Stop();
            Console.WriteLine("OMV Message System Serialization/Deserialization Passes: {0} Total time: {1}", TEST_ITER, timer.Elapsed.TotalSeconds);

            timer.Reset();
            timer.Start();
            for (int i = 0; i < TEST_ITER; ++i)
            {
                MemoryStream stream = new MemoryStream();
                xmlSerializer.Serialize(stream, s);
                stream.Flush();
                stream.Close();
            }

            timer.Stop();
            Console.WriteLine(".NET BinarySerialization/Deserialization Passes: {0} Total time: {1}", TEST_ITER, timer.Elapsed.TotalSeconds);
        }
        public void DirLandReplyMessage()
        {
            DirLandReplyMessage s = new DirLandReplyMessage();
            s.AgentID = UUID.Random();
            s.QueryID = UUID.Random();
            s.QueryReplies = new DirLandReplyMessage.QueryReply[2];

            DirLandReplyMessage.QueryReply q1 = new DirLandReplyMessage.QueryReply();
            q1.ActualArea = 1024;
            q1.Auction = true;
            q1.ForSale = true;
            q1.Name = "For Sale Parcel Q1";
            q1.ProductSku = "023";
            q1.SalePrice = 2193;
            q1.ParcelID = UUID.Random();

            s.QueryReplies[0] = q1;

            DirLandReplyMessage.QueryReply q2 = new DirLandReplyMessage.QueryReply();
            q2.ActualArea = 512;
            q2.Auction = true;
            q2.ForSale = true;
            q2.Name = "For Sale Parcel Q2";
            q2.ProductSku = "023";
            q2.SalePrice = 22193;
            q2.ParcelID = UUID.Random();

            s.QueryReplies[1] = q2;

            OSDMap map = s.Serialize();

            DirLandReplyMessage t = new DirLandReplyMessage();
            t.Deserialize(map);

            Assert.AreEqual(s.AgentID, t.AgentID);
            Assert.AreEqual(s.QueryID, t.QueryID);

            for (int i = 0; i < s.QueryReplies.Length; i++)
            {
                Assert.AreEqual(s.QueryReplies[i].ActualArea, t.QueryReplies[i].ActualArea);
                Assert.AreEqual(s.QueryReplies[i].Auction, t.QueryReplies[i].Auction);
                Assert.AreEqual(s.QueryReplies[i].ForSale, t.QueryReplies[i].ForSale);
                Assert.AreEqual(s.QueryReplies[i].Name, t.QueryReplies[i].Name);
                Assert.AreEqual(s.QueryReplies[i].ProductSku, t.QueryReplies[i].ProductSku);
                Assert.AreEqual(s.QueryReplies[i].ParcelID, t.QueryReplies[i].ParcelID);
                Assert.AreEqual(s.QueryReplies[i].SalePrice, t.QueryReplies[i].SalePrice);
            }
        }