private void MoveClientBinding(
            string instanceNameSource,
            string instanceNameTarget,
            string clientName
            )
        {
            IISBindings bindings = new IISBindings();

            ClientManager clientManager = new ClientManager(Path.Combine(
                                                                ConfigurationManager.AppSettings["InstanceRoot"],
                                                                instanceNameSource,
                                                                "App_Data",
                                                                "Clients.xml"
                                                                ));

            Client client = clientManager.GetSingle(clientName);

            bindings.RemoveBindings(
                instanceNameSource,
                client.Host
                );

            bindings.AddBindings(
                instanceNameTarget,
                client.Host
                );
        }
        private void CreateClient(HttpContext context)
        {
            // Get the client name from
            // the http request's parameters.
            string clientName = context.Request.Params["Client"];

            // Get the source instance name from
            // the http request's parameters.
            string instanceName = context.Request.Params["Instance"];

            string hostname = string.Format(
                ConfigurationManager.AppSettings["Hostname"],
                clientName
                );

            InstanceCollection instances = new InstanceCollection(
                ConfigurationManager.AppSettings["InstanceRoot"]
                );

            if (!instances.Instances.ContainsKey(instanceName))
            {
                context.Response.Write("__ERROR__Instance does not exist on the server.");
                return;
            }

            Instance instance = instances.Instances[instanceName];

            ClientManager clientManager = new ClientManager(Path.Combine(
                                                                ConfigurationManager.AppSettings["InstanceRoot"],
                                                                instanceName,
                                                                "App_Data",
                                                                "Clients.xml"
                                                                ));

            clientManager.Append(
                clientName.ToLower(),
                clientName,
                hostname,
                "#6CAEE0",
                "#FCB040",
                DateTime.Today.ToString("dd-MM-yyyy"),
                context.Request.Params["Servers"]
                );

            InstanceVersion version = new InstanceVersion(instance.Version);

            string databaseScriptsDirectory = Path.Combine(
                context.Request.PhysicalApplicationPath,
                "App_Data",
                "ClientCreation",
                version.ToString(),
                "DATABASE"
                );

            while (!Directory.Exists(databaseScriptsDirectory))
            {
                version -= 1;

                if (version.ToString() == "0.0.0.0")
                {
                    return;
                }

                databaseScriptsDirectory = Path.Combine(
                    context.Request.PhysicalApplicationPath,
                    "App_Data",
                    "ClientCreation",
                    version.ToString(),
                    "DATABASE"
                    );
            }

            string databaseRoot = ConfigurationManager.AppSettings["DatabaseRoot"];

            int i = 0;

            foreach (string scriptFile in Directory.GetFiles(databaseScriptsDirectory))
            {
                string script = File.ReadAllText(scriptFile);
                script = script.Replace("###CLIENTNAME###", clientName);
                script = script.Replace("###DATABASEROOT###", databaseRoot);

                ExecuteQuery(
                    i == 0 ? "master" : clientName,
                    script
                    );

                i++;
            }

            string fileSystemRoot = Path.Combine(
                context.Request.PhysicalApplicationPath,
                "App_Data",
                "ClientCreation",
                version.ToString(),
                "FILESYSTEM"
                );

            foreach (string directory in Directory.GetDirectories(fileSystemRoot))
            {
                string target = directory.Replace(
                    fileSystemRoot, Path.Combine(
                        ConfigurationManager.AppSettings["InstanceRoot"],
                        instanceName
                        ));

                CopyDirectory(
                    directory,
                    target,
                    clientName
                    );
            }

            try
            {
                IISBindings iisBindings = new IISBindings();
                iisBindings.AddBindings(instanceName, hostname);
            }
            catch { }

            string caseDataDirectory = Path.Combine(
                ConfigurationManager.AppSettings["CaseDataPath"],
                clientName
                );

            if (!Directory.Exists(caseDataDirectory))
            {
                Directory.CreateDirectory(caseDataDirectory);
            }

            ExecuteQuery(clientName,
                         "INSERT INTO [Roles] (Id, CreationDate, Name, Description) " +
                         "VALUES ({0}, {1}, {2}, {3})",
                         Guid.Parse("00000000-0000-0000-0000-000000000000"),
                         DateTime.Now,
                         "Administrators",
                         "Blueocean administrators"
                         );

            XmlDocument document = new XmlDocument();

            document.Load(Path.Combine(
                              ConfigurationManager.AppSettings["InstanceRoot"],
                              instanceName,
                              "App_Data",
                              "Permissions.xml"
                              ));

            foreach (XmlNode xmlNodePermission in document.SelectNodes("//Permission"))
            {
                ExecuteQuery(clientName,
                             "INSERT INTO [RolePermissions] (Id, IdRole, Permission, CreationDate) " +
                             "VALUES (NEWID(), {0}, {1}, {2})",
                             Guid.Parse("00000000-0000-0000-0000-000000000000"),
                             int.Parse(xmlNodePermission.Attributes["Id"].Value),
                             DateTime.Now
                             );
            }
        }