OSDMap LocalSimulationServiceConnector_OnMessageReceived(OSDMap message)
 {
     if (!message.ContainsKey("Method"))
         return null;
     switch (message["Method"].AsString())
     {
         case "CreateAgentRequest":
             CreateAgentRequest createAgentRequest = new CreateAgentRequest();
             createAgentRequest.FromOSD(message);
             CreateAgentResponse createAgentResponse = new CreateAgentResponse();
             createAgentResponse.Success = CreateAgent(createAgentRequest.Destination, createAgentRequest.CircuitData, createAgentRequest.TeleportFlags, out createAgentResponse);
             return createAgentResponse.ToOSD();
         case "UpdateAgentPositionRequest":
             UpdateAgentPositionRequest updateAgentPositionRequest = new UpdateAgentPositionRequest();
             updateAgentPositionRequest.FromOSD(message);
             return new OSDMap() { new KeyValuePair<string, OSD>("Success", UpdateAgent(updateAgentPositionRequest.Destination, updateAgentPositionRequest.Update)) };
         case "UpdateAgentDataRequest":
             UpdateAgentDataRequest updateAgentDataRequest = new UpdateAgentDataRequest();
             updateAgentDataRequest.FromOSD(message);
             return new OSDMap() { new KeyValuePair<string, OSD>("Success", UpdateAgent(updateAgentDataRequest.Destination, updateAgentDataRequest.Update)) };
         case "FailedToMoveAgentIntoNewRegionRequest":
             FailedToMoveAgentIntoNewRegionRequest failedToMoveAgentIntoNewRegionRequest = new FailedToMoveAgentIntoNewRegionRequest();
             failedToMoveAgentIntoNewRegionRequest.FromOSD(message);
             FailedToMoveAgentIntoNewRegion(failedToMoveAgentIntoNewRegionRequest.AgentID, failedToMoveAgentIntoNewRegionRequest.RegionID);
             break;
         case "CloseAgentRequest":
             CloseAgentRequest closeAgentRequest = new CloseAgentRequest();
             closeAgentRequest.FromOSD(message);
             CloseAgent(closeAgentRequest.Destination, closeAgentRequest.AgentID);
             break;
         case "MakeChildAgentRequest":
             MakeChildAgentRequest makeChildAgentRequest = new MakeChildAgentRequest();
             makeChildAgentRequest.FromOSD(message);
             MakeChildAgent(makeChildAgentRequest.AgentID, makeChildAgentRequest.Destination, makeChildAgentRequest.IsCrossing);
             break;
         case "FailedToTeleportAgentRequest":
             FailedToTeleportAgentRequest failedToTeleportAgentRequest = new FailedToTeleportAgentRequest();
             failedToTeleportAgentRequest.FromOSD(message);
             FailedToTeleportAgent(failedToTeleportAgentRequest.Destination, failedToTeleportAgentRequest.FailedRegionID,
                 failedToTeleportAgentRequest.AgentID, failedToTeleportAgentRequest.Reason, failedToTeleportAgentRequest.IsCrossing);
             break;
         case "RetrieveAgentRequest":
             RetrieveAgentRequest retrieveAgentRequest = new RetrieveAgentRequest();
             retrieveAgentRequest.FromOSD(message);
             RetrieveAgentResponse retrieveAgentResponse = new RetrieveAgentResponse();
             retrieveAgentResponse.Success = RetrieveAgent(retrieveAgentRequest.Destination, retrieveAgentRequest.AgentID, retrieveAgentRequest.AgentIsLeaving,
                 out retrieveAgentResponse.AgentData, out retrieveAgentResponse.CircuitData);
             return retrieveAgentResponse.ToOSD();
         case "CreateObjectRequest":
             CreateObjectRequest createObjectRequest = new CreateObjectRequest();
             createObjectRequest.Scene = Scene;
             createObjectRequest.FromOSD(message);
             return new OSDMap() { new KeyValuePair<string, OSD>("Success", CreateObject(createObjectRequest.Destination, createObjectRequest.Object)) };
     }
     return null;
 }
        public bool UpdateAgent(GridRegion destination, AgentPosition data)
        {
            if (m_blackListedRegions.ContainsKey(destination.ServerURI))
            {
                //Check against time
                if (m_blackListedRegions[destination.ServerURI] > 3 &&
                    Util.EnvironmentTickCountSubtract(m_blackListedRegions[destination.ServerURI]) > 0)
                {
                    MainConsole.Instance.Warn("[SimServiceConnector]: Blacklisted region " + destination.RegionName +
                                              " requested");
                    //Still blacklisted
                    return false;
                }
            }

            UpdateAgentPositionRequest request = new UpdateAgentPositionRequest();
            request.Update = data;
            request.Destination = destination;

            AutoResetEvent resetEvent = new AutoResetEvent(false);
            OSDMap result = null;
            m_syncMessagePoster.Get(destination.ServerURI, request.ToOSD(), (response) =>
            {
                result = response;
                resetEvent.Set();
            });
            bool success = resetEvent.WaitOne(10000) && result != null;
            if (!success)
            {
                if (m_blackListedRegions.ContainsKey(destination.ServerURI))
                {
                    if (m_blackListedRegions[destination.ServerURI] == 3)
                    {
                        //add it to the blacklist as the request completely failed 3 times
                        m_blackListedRegions[destination.ServerURI] = Util.EnvironmentTickCount() + 60 * 1000; //60 seconds
                    }
                    else if (m_blackListedRegions[destination.ServerURI] == 0)
                        m_blackListedRegions[destination.ServerURI]++;
                }
                else
                    m_blackListedRegions[destination.ServerURI] = 0;
                return false;
            }

            //Clear out the blacklist if it went through
            m_blackListedRegions.Remove(destination.ServerURI);

            return result["Success"].AsBoolean();
        }