/// <summary>
        ///   Register this region with the grid service
        /// </summary>
        /// <param name = "scene"></param>
        /// <param name = "returnResponseFirstTime">Should we try to walk the user through what went wrong?</param>
        /// <param name="continueTrying"> </param>
        /// <param name="password"> </param>
        public bool RegisterRegionWithGrid(IScene scene, bool returnResponseFirstTime, bool continueTrying, string password)
        {
            GridRegion region = BuildGridRegion(scene.RegionInfo);

            IGenericsConnector g           = DataManager.DataManager.RequestPlugin <IGenericsConnector>();
            GridSessionID      s           = null;
            IGridService       GridService = scene.RequestModuleInterface <IGridService>();

            if (g != null) //Get the sessionID from the database if possible
            {
                s = g.GetGeneric <GridSessionID>(scene.RegionInfo.RegionID, "GridSessionID", "GridSessionID");
            }

            if (s == null)
            {
                s = new GridSessionID {
                    SessionID = scene.RegionInfo.GridSecureSessionID
                };
                //Set it from the regionInfo if it knows anything
            }

            scene.RequestModuleInterface <ISimulationBase>().EventManager.FireGenericEventHandler("PreRegisterRegion", region);

            //Tell the grid service about us
            RegisterRegion error = GridService.RegisterRegion(region, s.SessionID, password);

            if (error.Error == String.Empty)
            {
                s.SessionID = error.SessionID;
                //If it registered ok, we save the sessionID to the database and tlel the neighbor service about it
                scene.RegionInfo.GridSecureSessionID = error.SessionID;
                //Update our local copy of what our region flags are
                scene.RegionInfo.RegionFlags = error.RegionFlags;
                scene.RegionInfo.ScopeID     = error.Region.ScopeID;
                scene.RegionInfo.AllScopeIDs = error.Region.AllScopeIDs;

                //Save the new SessionID to the database
                if (g != null)
                {
                    g.AddGeneric(scene.RegionInfo.RegionID, "GridSessionID", "GridSessionID", s.ToOSD());
                }

                m_knownNeighbors[scene.RegionInfo.RegionID] = error.Neighbors;
                return(true); //Success
            }
            if (returnResponseFirstTime && !continueTrying)
            {
                MainConsole.Instance.Error("[RegisterRegionWithGrid]: Registration of region with grid failed again - " + error.Error);
                return(false);
            }

            //Parse the error and try to do something about it if at all possible
            if (error.Error == "Region location is reserved")
            {
                MainConsole.Instance.Error(
                    "[RegisterRegionWithGrid]: Registration of region with grid failed - The region location you specified is reserved. You must move your region.");
                int X = 0, Y = 0;
                int.TryParse(MainConsole.Instance.Prompt("New Region Location X", "1000"), out X);
                int.TryParse(MainConsole.Instance.Prompt("New Region Location Y", "1000"), out Y);

                scene.RegionInfo.RegionLocX = X * Constants.RegionSize;
                scene.RegionInfo.RegionLocY = Y * Constants.RegionSize;

                IRegionLoader[] loaders = scene.RequestModuleInterfaces <IRegionLoader>();
                foreach (IRegionLoader loader in loaders)
                {
                    loader.UpdateRegionInfo(scene.RegionInfo.RegionName, scene.RegionInfo);
                }
            }
            else if (error.Error == "Region overlaps another region")
            {
                MainConsole.Instance.Error("[RegisterRegionWithGrid]: Registration of region " + scene.RegionInfo.RegionName +
                                           " with the grid failed - The region location you specified is already in use. You must move your region.");
                int X = 0, Y = 0;
                int.TryParse(
                    MainConsole.Instance.Prompt("New Region Location X",
                                                (scene.RegionInfo.RegionLocX / 256).ToString()), out X);
                int.TryParse(
                    MainConsole.Instance.Prompt("New Region Location Y",
                                                (scene.RegionInfo.RegionLocY / 256).ToString()), out Y);

                scene.RegionInfo.RegionLocX = X * Constants.RegionSize;
                scene.RegionInfo.RegionLocY = Y * Constants.RegionSize;

                IRegionLoader[] loaders = scene.RequestModuleInterfaces <IRegionLoader>();
                foreach (IRegionLoader loader in loaders)
                {
                    loader.UpdateRegionInfo(scene.RegionInfo.RegionName, scene.RegionInfo);
                }
            }
            else if (error.Error.Contains("Can't move this region"))
            {
                MainConsole.Instance.Error("[RegisterRegionWithGrid]: Registration of region " + scene.RegionInfo.RegionName +
                                           " with the grid failed - You can not move this region. Moving it back to its original position.");
                //Opensim Grid Servers don't have this functionality.
                try
                {
                    string[] position = error.Error.Split(',');

                    scene.RegionInfo.RegionLocX = int.Parse(position[1]) * Constants.RegionSize;
                    scene.RegionInfo.RegionLocY = int.Parse(position[2]) * Constants.RegionSize;

                    IRegionLoader[] loaders = scene.RequestModuleInterfaces <IRegionLoader>();
                    foreach (IRegionLoader loader in loaders)
                    {
                        loader.UpdateRegionInfo(scene.RegionInfo.RegionName, scene.RegionInfo);
                    }
                }
                catch (Exception e)
                {
                    MainConsole.Instance.Error(
                        "Unable to move the region back to its original position, is this an opensim server? Please manually move the region back.");
                    throw e;
                }
            }
            else if (error.Error == "Duplicate region name")
            {
                MainConsole.Instance.Error("[RegisterRegionWithGrid]: Registration of region " + scene.RegionInfo.RegionName +
                                           " with the grid failed - The region name you specified is already in use. Please change the name.");
                string oldRegionName = scene.RegionInfo.RegionName;
                scene.RegionInfo.RegionName = MainConsole.Instance.Prompt("New Region Name", "");

                IRegionLoader[] loaders = scene.RequestModuleInterfaces <IRegionLoader>();
                foreach (IRegionLoader loader in loaders)
                {
                    loader.UpdateRegionInfo(oldRegionName, scene.RegionInfo);
                }
            }
            else if (error.Error == "Region locked out")
            {
                MainConsole.Instance.Error("[RegisterRegionWithGrid]: Registration of region " + scene.RegionInfo.RegionName +
                                           " with the grid the failed - The region you are attempting to join has been blocked from connecting. Please connect another region.");
                MainConsole.Instance.Prompt("Press enter when you are ready to exit");
                Environment.Exit(0);
            }
            else if (error.Error == "Could not reach grid service")
            {
                MainConsole.Instance.Error("[RegisterRegionWithGrid]: Registration of region " + scene.RegionInfo.RegionName +
                                           " with the grid failed - The grid service can not be found! Please make sure that you can connect to the grid server and that the grid server is on.");
                MainConsole.Instance.Error(
                    "You should also make sure you've provided the correct address and port of the grid service.");
                string input =
                    MainConsole.Instance.Prompt(
                        "Press enter when you are ready to proceed, or type cancel to exit");
                if (input == "cancel")
                {
                    Environment.Exit(0);
                }
            }
            else if (error.Error == "Wrong Session ID")
            {
                MainConsole.Instance.Error("[RegisterRegionWithGrid]: Registration of region " + scene.RegionInfo.RegionName +
                                           " with the grid failed - Wrong Session ID for this region!");
                MainConsole.Instance.Error(
                    "This means that this region has failed to connect to the grid server and needs removed from it before it can connect again.");
                MainConsole.Instance.Error(
                    "If you are running the Aurora.Server instance this region is connecting to, type \"clear grid region <RegionName>\" and then press enter on this console and it will work");
                MainConsole.Instance.Error(
                    "If you are not running the Aurora.Server instance this region is connecting to, please contact your grid operator so that he can fix it");

                string input =
                    MainConsole.Instance.Prompt(
                        "Press enter when you are ready to proceed, or type cancel to exit");
                if (input == "cancel")
                {
                    Environment.Exit(0);
                }
            }
            else
            {
                MainConsole.Instance.Error("[RegisterRegionWithGrid]: Registration of region " + scene.RegionInfo.RegionName +
                                           " with the grid failed - " + error.Error + "!");
                string input =
                    MainConsole.Instance.Prompt(
                        "Press enter when you are ready to proceed, or type cancel to exit");
                if (input == "cancel")
                {
                    Environment.Exit(0);
                }
            }
            return(RegisterRegionWithGrid(scene, true, continueTrying, password));
        }
        /// <summary>
        ///     Register this region with the grid service
        /// </summary>
        /// <param name="scene"></param>
        /// <param name="returnResponseFirstTime">Should we try to walk the user through what went wrong?</param>
        /// <param name="continueTrying"> </param>
        /// <param name="password"> </param>
        public bool RegisterRegionWithGrid(IScene scene, bool returnResponseFirstTime, bool continueTrying,
                                           string password)
        {
            if (password == null)
            {
                password = m_RegisterRegionPassword;
            }

            GridRegion   region      = new GridRegion(scene.RegionInfo);
            IGridService GridService = scene.RequestModuleInterface <IGridService> ();

            scene.RequestModuleInterface <ISimulationBase> ()
            .EventManager.FireGenericEventHandler("PreRegisterRegion", region);

            // Tell the grid service about us
            RegisterRegion error = GridService.RegisterRegion(region, scene.RegionInfo.GridSecureSessionID, password,
                                                              ProtocolVersion.MAJOR_PROTOCOL_VERSION,
                                                              ProtocolVersion.MINOR_PROTOCOL_VERSION);

            if (error.Error == string.Empty)
            {
                // If it registered ok, we save the sessionID to the database and tell the neighbor service about it
                scene.RegionInfo.GridSecureSessionID = error.SessionID;
                // Update our local copy of what our region flags are
                scene.RegionInfo.RegionFlags = error.RegionFlags;
                scene.RegionInfo.ScopeID     = error.Region.ScopeID;
                scene.RegionInfo.AllScopeIDs = error.Region.AllScopeIDs;
                scene.RequestModuleInterface <IConfigurationService> ().SetURIs(error.URIs);
                m_knownNeighbors [scene.RegionInfo.RegionID] = error.Neighbors;

                return(true);    // Success
            }

            if (returnResponseFirstTime && !continueTrying)
            {
                MainConsole.Instance.Error(
                    "[RegisterRegionWithGrid]: Registration of region with grid failed again - " + error.Error);

                return(false);
            }

            // something failed...
            string errmsg = "[RegisterRegionWithGrid]: Registration of region " + scene.RegionInfo.RegionName + " with the grid failed!";

            // Parse the error and try to do something about it if at all possible
            if (error.Error == "Region location is reserved")
            {
                var simbase = scene.RequestModuleInterface <ISimulationBase> ();

                MainConsole.Instance.Error(errmsg);
                MainConsole.Instance.Error("    The region location you specified is reserved. You must move your region.");
                int X;
                int Y;
                int.TryParse(MainConsole.Instance.Prompt("New Region Location X", simbase.MapCenterX.ToString()), out X);
                int.TryParse(MainConsole.Instance.Prompt("New Region Location Y", simbase.MapCenterY.ToString()), out Y);

                scene.RegionInfo.RegionLocX = X * Constants.RegionSize;
                scene.RegionInfo.RegionLocY = Y * Constants.RegionSize;
            }
            else if (error.Error == "Region overlaps another region")
            {
                MainConsole.Instance.Error(errmsg);
                MainConsole.Instance.Error("    The region location you specified is already in use. You must move your region.");
                int X;
                int Y;
                int.TryParse(
                    MainConsole.Instance.Prompt("New Region Location X",
                                                (scene.RegionInfo.RegionLocX / Constants.RegionSize).ToString()), out X);
                int.TryParse(
                    MainConsole.Instance.Prompt("New Region Location Y",
                                                (scene.RegionInfo.RegionLocY / Constants.RegionSize).ToString()), out Y);

                scene.RegionInfo.RegionLocX = X * Constants.RegionSize;
                scene.RegionInfo.RegionLocY = Y * Constants.RegionSize;
            }
            else if (error.Error.Contains("Can't move this region"))
            {
                MainConsole.Instance.Error(errmsg);
                MainConsole.Instance.Error("    You can not move this region. Moving it back to its original position.");
                // Opensim Grid Servers don't have this functionality.
                try {
                    string [] position = error.Error.Split(',');

                    scene.RegionInfo.RegionLocX = int.Parse(position [1]) * Constants.RegionSize;
                    scene.RegionInfo.RegionLocY = int.Parse(position [2]) * Constants.RegionSize;
                } catch (Exception) {
                    MainConsole.Instance.Error("Unable to move the region back to its original position, is this an Opensim server?");
                    MainConsole.Instance.Error("Please manually move the region back.");
                    throw;
                }
            }
            else if (error.Error == "Duplicate region name")
            {
                MainConsole.Instance.Error(errmsg);
                MainConsole.Instance.Error("    The region name you specified is already in use. Please change the name.");
                scene.RegionInfo.RegionName = MainConsole.Instance.Prompt("New Region Name", "");
            }
            else if (error.Error == "Region locked out")
            {
                MainConsole.Instance.Error(errmsg);
                MainConsole.Instance.Error("    The region you are attempting to join has been blocked from connecting.");
                MainConsole.Instance.Error("    Please connect another region.");
                MainConsole.Instance.Prompt("WhiteCore will exit. Press enter when you are ready to exit");
                Environment.Exit(0);
            }
            else if (error.Error == "Could not reach grid service")
            {
                MainConsole.Instance.Error(errmsg);
                MainConsole.Instance.Error("     The grid service can not be found!");
                MainConsole.Instance.Error("     Please make sure that you can connect to the grid server and that the grid server is on.");
                MainConsole.Instance.Error("     You should also make sure you've provided the correct address and port of the grid service.");
                string input =
                    MainConsole.Instance.Prompt("Press enter when you are ready to proceed, or type 'cancel' to exit");
                if (input == "cancel")
                {
                    Environment.Exit(0);
                }
            }
            else if (error.Error == "Wrong Session ID")
            {
                MainConsole.Instance.Error(errmsg);
                MainConsole.Instance.Error("    Wrong Session ID for this region!");
                MainConsole.Instance.Error("    This means that this region has failed to connect to the grid server");
                MainConsole.Instance.Error("    and needs removed from it before it can connect again.\n");
                MainConsole.Instance.Error("    If you are running the WhiteCore.Server instance this region is connecting to,");
                MainConsole.Instance.Error("    type \"grid clear region <RegionName>\" and then press enter on this console and it will work\n");
                MainConsole.Instance.Error("    If you are not running the WhiteCore.Server instance this region is connecting to,");
                MainConsole.Instance.Error("    please contact your grid operator so that the problem can be corrected");

                string input =
                    MainConsole.Instance.Prompt("Press enter when you are ready to proceed, or type cancel to exit");
                if (input == "cancel")
                {
                    Environment.Exit(0);
                }
            }
            else if (error.Error == "Grid is not fully ready yet, please try again shortly")
            {
                MainConsole.Instance.Error(errmsg + "- " + error.Error + ", retrying... ");
                System.Threading.Thread.Sleep(3000);    // Sleep for a bit and try again
            }
            else
            {
                MainConsole.Instance.Error(errmsg + " - " + error.Error + "!");
                string input =
                    MainConsole.Instance.Prompt("Press enter when you are ready to proceed, or type 'cancel' to exit");
                if (input == "cancel")
                {
                    Environment.Exit(0);
                }
            }

            return(RegisterRegionWithGrid(scene, true, continueTrying, password));
        }