public static int DeleteWebSitePointer(int siteItemId, int domainId, bool updateWebSite)
        {
            // check account
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive);
            if (accountCheck < 0) return accountCheck;

            // load site item
            WebSite siteItem = (WebSite)PackageController.GetPackageItem(siteItemId);
            if (siteItem == null)
                return BusinessErrorCodes.ERROR_WEB_SITE_PACKAGE_ITEM_NOT_FOUND;

            // load domain item
            DomainInfo domain = ServerController.GetDomain(domainId);
            if (domain == null)
                return BusinessErrorCodes.ERROR_DOMAIN_PACKAGE_ITEM_NOT_FOUND;

            // load appropriate zone
            DnsZone zone = (DnsZone)PackageController.GetPackageItem(domain.ZoneItemId);

            // get zone records for the service
            List<GlobalDnsRecord> dnsRecords = ServerController.GetDnsRecordsByService(siteItem.ServiceId);

            // load web site IP address
            IPAddressInfo ip = ServerController.GetIPAddress(siteItem.SiteIPAddressId);

            // place log record
            TaskManager.StartTask("WEB_SITE", "DELETE_POINTER", siteItem.Name);
            TaskManager.ItemId = siteItemId;
            TaskManager.WriteParameter("Domain pointer", domain.DomainName);

            try
            {
                if (zone != null)
                {
                    // change DNS zone
                    string serviceIp = (ip != null) ? ip.ExternalIP : null;

                    List<DnsRecord> resourceRecords = DnsServerController.BuildDnsResourceRecords(
                        dnsRecords, domain.DomainName, serviceIp);

                    try
                    {
                        DNSServer dns = new DNSServer();
                        ServiceProviderProxy.Init(dns, zone.ServiceId);
                        dns.DeleteZoneRecords(zone.Name, resourceRecords.ToArray());
                    }
                    catch(Exception ex1)
                    {
                        TaskManager.WriteError(ex1, "Error deleting DNS records");
                    }
                }

                if (updateWebSite)
                {
                    // get existing web site bindings
                    WebServer web = new WebServer();
                    ServiceProviderProxy.Init(web, siteItem.ServiceId);

                    List<ServerBinding> bindings = new List<ServerBinding>();
                    bindings.AddRange(web.GetSiteBindings(siteItem.SiteId));

                    // check if web site has dedicated IP assigned
                    bool dedicatedIp = bindings.Exists(binding => { return String.IsNullOrEmpty(binding.Host) && binding.IP != "*"; });

                    // update binding only for "shared" ip addresses
                    if (!dedicatedIp)
                    {
                        // remove host headers
                        List<ServerBinding> domainBindings = new List<ServerBinding>();
                        FillWebServerBindings(domainBindings, dnsRecords, "", domain.DomainName);

                        // fill to remove list
                        List<string> headersToRemove = new List<string>();
                        foreach (ServerBinding domainBinding in domainBindings)
                            headersToRemove.Add(domainBinding.Host);

                        // remove bndings
                        bindings.RemoveAll(b => { return headersToRemove.Contains(b.Host) && b.Port == "80"; } );

                        // update bindings
                        web.UpdateSiteBindings(siteItem.SiteId, bindings.ToArray());
                    }
                }

                // update domain
                domain.WebSiteId = 0;
                ServerController.UpdateDomain(domain);

                return 0;
            }
            catch (Exception ex)
            {
                throw TaskManager.WriteError(ex);
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }
        public static int SwitchWebSiteToSharedIP(int siteItemId)
        {
            // check account
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive);
            if (accountCheck < 0) return accountCheck;

            // load web site item
            WebSite siteItem = (WebSite)PackageController.GetPackageItem(siteItemId);
            if (siteItem == null)
                return BusinessErrorCodes.ERROR_WEB_SITE_PACKAGE_ITEM_NOT_FOUND;



            // place log record
            TaskManager.StartTask("WEB_SITE", "SWITCH_TO_SHARED_IP", siteItem.Name, siteItemId);

            try
            {
                IPAddressInfo ip;

                DomainInfo domain = ServerController.GetDomain(siteItem.Name);
                DomainInfo ZoneInfo = ServerController.GetDomain(domain.DomainItemId);

                if (ZoneInfo == null)
                    throw new Exception("Parent zone not found");


                if (ZoneInfo.ZoneItemId > 0)
                {
                    StringDictionary settings = ServerController.GetServiceSettings(siteItem.ServiceId);
                    if (string.IsNullOrEmpty(settings["PublicSharedIP"]))
                        return BusinessErrorCodes.ERROR_PUBLICSHAREDIP_FOR_SHAREDIP;

                }

                //cleanup certificates
                List<SSLCertificate> certificates = GetCertificatesForSite(siteItemId);
                foreach (SSLCertificate c in certificates)
                {
                    DeleteCertificate(siteItemId, c);
                }

                certificates = GetPendingCertificates(siteItemId);
                foreach (SSLCertificate c in certificates)
                {
                    DeleteCertificateRequest(siteItemId, c.id);
                }

                // remove all web site pointers
                List<DomainInfo> pointers = GetWebSitePointers(siteItemId);
                foreach (DomainInfo pointer in pointers)
                    DeleteWebSitePointer(siteItemId, pointer.DomainId, true, true, false);

                // remove web site main pointer
                if (domain != null)
                    DeleteWebSitePointer(siteItemId, domain.DomainId, true, true, false);

                // clear binding left overs
                WebServer web = new WebServer();
                ServiceProviderProxy.Init(web, siteItem.ServiceId);
                List<ServerBinding> newBindings = new List<ServerBinding>();

                try
                {
                    web.UpdateSiteBindings(siteItem.SiteId, newBindings.ToArray(), true);
                }
                catch (Exception)
                {

                }


                List<WebSite> sites = WebServerController.GetWebSites(domain.PackageId, false);
                bool oneSiteOnly = (sites.Count == 1);

                if (oneSiteOnly)
                {
                    // load site item
                    ip = ServerController.GetIPAddress(sites[0].SiteIPAddressId);

                    string serviceIp = (ip != null) ? ip.ExternalIP : null;

                    if (string.IsNullOrEmpty(serviceIp))
                    {
                        StringDictionary settings = ServerController.GetServiceSettings(sites[0].ServiceId);
                        if (settings["PublicSharedIP"] != null)
                            serviceIp = settings["PublicSharedIP"].ToString();
                    }

                    ServerController.RemoveServiceDNSRecords(domain.PackageId, ResourceGroups.Web, domain, serviceIp, true);
                }


                //figure out the PackageAddressId
                PackageIPAddress packageIpAddress = null;
                var siteIpAddresses = ServerController.GetItemIPAddresses(siteItemId, IPAddressPool.None);
                foreach (var siteIp in siteIpAddresses)
                {
                    packageIpAddress = ServerController.GetPackageIPAddress(siteIp.AddressID);
                    if (packageIpAddress != null && packageIpAddress.AddressID == siteItem.SiteIPAddressId)
                    {
                        break;
                    }
                }
                
                //Deallocate IP Address
                if (packageIpAddress != null)
                    ServerController.DeleteItemIPAddress(siteItemId, packageIpAddress.PackageAddressID);

                // update site item
                siteItem.SiteIPAddressId = 0;
                PackageController.UpdatePackageItem(siteItem);

                string parentZone = domain.ZoneName;
                if (string.IsNullOrEmpty(parentZone))
                {
                    DomainInfo parentDomain = ServerController.GetDomain(domain.DomainItemId);
                    parentZone = parentDomain.DomainName;
                }

                AddWebSitePointer(siteItemId,
                    ((domain.DomainName.Replace("." + parentZone, "") == parentZone) |
                    (domain.DomainName == parentZone))
                    ? "" : domain.DomainName.Replace("." + parentZone, "")
                    , ZoneInfo.DomainId, true, true, true);

                foreach (DomainInfo pointer in pointers)
                {
                    string pointerParentZone = pointer.ZoneName;
                    if (string.IsNullOrEmpty(pointerParentZone))
                    {
                        DomainInfo parentDomain = ServerController.GetDomain(pointer.DomainItemId);
                        pointerParentZone = parentDomain.DomainName;
                    }


                    ZoneInfo = ServerController.GetDomain(pointerParentZone);

                    AddWebSitePointer(siteItemId,
                        ((pointer.DomainName.Replace("." + pointerParentZone, "") == pointerParentZone) |
                        (pointer.DomainName == pointerParentZone))
                        ? "" : pointer.DomainName.Replace("." + pointerParentZone, "")
                        , ZoneInfo.DomainId, true, true, true);
                }

                if (oneSiteOnly)
                {
                    sites = WebServerController.GetWebSites(domain.PackageId, false);

                    // load site item
                    ip = ServerController.GetIPAddress(sites[0].SiteIPAddressId);

                    string serviceIp = (ip != null) ? ip.ExternalIP : null;

                    if (string.IsNullOrEmpty(serviceIp))
                    {
                        StringDictionary settings = ServerController.GetServiceSettings(sites[0].ServiceId);
                        if (settings["PublicSharedIP"] != null)
                            serviceIp = settings["PublicSharedIP"].ToString();
                    }

                    ServerController.AddServiceDNSRecords(domain.PackageId, ResourceGroups.Web, domain, serviceIp, true);
                }


                return 0;
            }
            catch (Exception ex)
            {
                throw TaskManager.WriteError(ex);
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }
        public static int SwitchWebSiteToDedicatedIP(int siteItemId, int ipAddressId)
        {
            // check account
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive);
            if (accountCheck < 0) return accountCheck;

            // load web site item
            WebSite siteItem = (WebSite)PackageController.GetPackageItem(siteItemId);
            if (siteItem == null)
                return BusinessErrorCodes.ERROR_WEB_SITE_PACKAGE_ITEM_NOT_FOUND;

            
            int addressId = 0;
            PackageIPAddress packageIp = ServerController.GetPackageIPAddress(ipAddressId);
            if (packageIp != null)
            {
                addressId = packageIp.AddressID;
            }

            List<GlobalDnsRecord> dnsRecords = ServerController.GetDnsRecordsByService(siteItem.ServiceId);

            foreach (GlobalDnsRecord d in dnsRecords)
            {
                if (!string.IsNullOrEmpty(d.ExternalIP))
                {
                    if (!IsValidIPAdddress(d.ExternalIP)) return BusinessErrorCodes.ERROR_GLOBALDNS_FOR_DEDICATEDIP;
                }
            }

            // place log record
            TaskManager.StartTask("WEB_SITE", "SWITCH_TO_DEDICATED_IP", siteItem.Name, siteItemId);

            try
            {
                IPAddressInfo ip;

                // remove all web site pointers
                DomainInfo domain = ServerController.GetDomain(siteItem.Name);
                DomainInfo ZoneInfo = ServerController.GetDomain(domain.DomainItemId);

                if (ZoneInfo == null)
                    throw new Exception("Parent zone not found");

                //cleanup certificates
                List<SSLCertificate> certificates = GetCertificatesForSite(siteItemId);
                foreach (SSLCertificate c in certificates)
                {
                    DeleteCertificate(siteItemId, c);
                }

                certificates = GetPendingCertificates(siteItemId);
                foreach (SSLCertificate c in certificates)
                {
                    DeleteCertificateRequest(siteItemId, c.id);
                }
                
                List<DomainInfo> pointers = GetWebSitePointers(siteItemId);
                foreach (DomainInfo pointer in pointers)
                    DeleteWebSitePointer(siteItemId, pointer.DomainId, true, true, false);

                // remove web site main pointer
                if (domain != null)
                    DeleteWebSitePointer(siteItemId, domain.DomainId, true, true, false);

                // clear binding left overs
                List<ServerBinding> newBindings = new List<ServerBinding>();
                WebServer web = new WebServer();
                ServiceProviderProxy.Init(web, siteItem.ServiceId);
                try
                {
                    web.UpdateSiteBindings(siteItem.SiteId, newBindings.ToArray(), true);
                }
                catch (Exception)
                {

                }

                List<WebSite> sites = WebServerController.GetWebSites(domain.PackageId, false);
                bool oneSiteOnly = (sites.Count == 1);

                if (oneSiteOnly)
                {
                    // load site item
                    ip = ServerController.GetIPAddress(sites[0].SiteIPAddressId);

                    string serviceIp = (ip != null) ? ip.ExternalIP : null;

                    if (string.IsNullOrEmpty(serviceIp))
                    {
                        StringDictionary settings = ServerController.GetServiceSettings(sites[0].ServiceId);
                        if (settings["PublicSharedIP"] != null)
                            serviceIp = settings["PublicSharedIP"].ToString();
                    }

                    ServerController.RemoveServiceDNSRecords(domain.PackageId, ResourceGroups.Web, domain, serviceIp, true);
                }


                // update site item
                siteItem.SiteIPAddressId = addressId;
                PackageController.UpdatePackageItem(siteItem);

                // associate IP with web site
                ServerController.AddItemIPAddress(siteItemId, ipAddressId);


                string parentZone = domain.ZoneName;
                if (string.IsNullOrEmpty(parentZone))
                {
                    DomainInfo parentDomain = ServerController.GetDomain(domain.DomainItemId);
                    parentZone = parentDomain.DomainName;
                }


                AddWebSitePointer(siteItemId,
                    ((domain.DomainName.Replace("." + parentZone, "") == parentZone) |
                    (domain.DomainName == parentZone))
                    ? "" : domain.DomainName.Replace("." + parentZone, "")
                    , ZoneInfo.DomainId, true, true, true);

                foreach (DomainInfo pointer in pointers)
                {
                    string pointerParentZone = pointer.ZoneName;
                    if (string.IsNullOrEmpty(pointerParentZone))
                    {
                        DomainInfo parentDomain = ServerController.GetDomain(pointer.DomainItemId);
                        pointerParentZone = parentDomain.DomainName;
                    }


                    ZoneInfo = ServerController.GetDomain(pointerParentZone);

                    AddWebSitePointer(siteItemId,
                        ((pointer.DomainName.Replace("." + pointerParentZone, "") == pointerParentZone)  |
                        (pointer.DomainName == pointerParentZone))
                        ? "" : pointer.DomainName.Replace("." + pointerParentZone, "")
                        , ZoneInfo.DomainId, true, true, true);
                }

                // load web site IP address
                ip = ServerController.GetIPAddress(siteItem.SiteIPAddressId);
                string ipAddr = "*";
                if (ip != null)
                    ipAddr = !String.IsNullOrEmpty(ip.InternalIP) ? ip.InternalIP : ip.ExternalIP;

                newBindings = new List<ServerBinding>();

                ServerBinding srvBinding = new ServerBinding(ipAddr, "80", "");
                newBindings.Add(srvBinding);

                foreach (ServerBinding b in web.GetSiteBindings(siteItem.SiteId))
                {
                    if (!((b.Host == srvBinding.Host) &
                        (b.IP == srvBinding.IP) &
                        (b.Port == srvBinding.Port)))
                        newBindings.Add(b);
                }
               
                web.UpdateSiteBindings(siteItem.SiteId, newBindings.ToArray(), false);

                if (oneSiteOnly)
                {
                    sites = WebServerController.GetWebSites(domain.PackageId, false);

                    // load site item
                    ip = ServerController.GetIPAddress(sites[0].SiteIPAddressId);

                    string serviceIp = (ip != null) ? ip.ExternalIP : null;

                    if (string.IsNullOrEmpty(serviceIp))
                    {
                        StringDictionary settings = ServerController.GetServiceSettings(sites[0].ServiceId);
                        if (settings["PublicSharedIP"] != null)
                            serviceIp = settings["PublicSharedIP"].ToString();
                    }

                    ServerController.AddServiceDNSRecords(domain.PackageId, ResourceGroups.Web, domain, serviceIp, true);
                }
                
                return 0;
            }
            catch (Exception ex)
            {
                throw TaskManager.WriteError(ex);
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }
        internal static int AddWebSitePointer(int siteItemId, string hostName, int domainId, bool updateWebSite, bool ignoreGlobalDNSRecords, bool rebuild)
        {
            // check account
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive);
            if (accountCheck < 0) return accountCheck;

            // load site item
            WebSite siteItem = (WebSite)PackageController.GetPackageItem(siteItemId);
            if (siteItem == null)
                return BusinessErrorCodes.ERROR_WEB_SITE_PACKAGE_ITEM_NOT_FOUND;

            // load domain item
            DomainInfo domain = ServerController.GetDomain(domainId);
            if (domain == null)
                return BusinessErrorCodes.ERROR_DOMAIN_PACKAGE_ITEM_NOT_FOUND;

            // check if the web site already exists
            if (!rebuild)
            {
                if (DataProvider.CheckDomain(domain.PackageId, string.IsNullOrEmpty(hostName) ? domain.DomainName : hostName + "." + domain.DomainName, true) != 0)
                    return BusinessErrorCodes.ERROR_WEB_SITE_ALREADY_EXISTS;
            }

            // get zone records for the service
            List<GlobalDnsRecord> dnsRecords = ServerController.GetDnsRecordsByService(siteItem.ServiceId);

            // load web site IP address
            IPAddressInfo ip = ServerController.GetIPAddress(siteItem.SiteIPAddressId);

            // place log record
            TaskManager.StartTask("WEB_SITE", "ADD_POINTER", siteItem.Name, siteItemId);

            TaskManager.WriteParameter("Domain pointer", domain.DomainName);
            TaskManager.WriteParameter("Host name", hostName);
            TaskManager.WriteParameter("updateWebSite", updateWebSite.ToString());

            try
            {

               
                // load appropriate zone
                DnsZone zone = (DnsZone)PackageController.GetPackageItem(domain.ZoneItemId);
                               

                if (zone != null)
                {
                    // change DNS zone
                    List<GlobalDnsRecord> tmpDnsRecords = new List<GlobalDnsRecord>();

                    string serviceIp = (ip != null) ? ip.ExternalIP : null;

                    if (string.IsNullOrEmpty(serviceIp))
                    {
                        StringDictionary settings = ServerController.GetServiceSettings(siteItem.ServiceId);
                        if (settings["PublicSharedIP"] != null)
                            serviceIp = settings["PublicSharedIP"].ToString();
                    }

                    //filter initiat GlobaDNSRecords list
                    if (ignoreGlobalDNSRecords)
                    {
                        //ignore all other except the host_name record
                        foreach (GlobalDnsRecord r in dnsRecords)
                        {
                            if (r.RecordName == "[host_name]")
                            {
                                tmpDnsRecords.Add(r);
                                break;
                            }
                        }
                    }
                    else
                        tmpDnsRecords = dnsRecords;


                    List<DnsRecord> resourceRecords = DnsServerController.BuildDnsResourceRecords(tmpDnsRecords, hostName, domain.DomainName, serviceIp);

                    if (!rebuild)
                    {
                        foreach (DnsRecord r in resourceRecords)
                        {
                            if (r.RecordName != "*")
                            {
                                // check if the web site already exists
                                if (DataProvider.CheckDomain(domain.PackageId, string.IsNullOrEmpty(r.RecordName) ? domain.DomainName : r.RecordName + "." + domain.DomainName, true) != 0)
                                    return BusinessErrorCodes.ERROR_WEB_SITE_ALREADY_EXISTS;
                            }
                        }
                    }

                    try
                    {
                        DNSServer dns = new DNSServer();
                        ServiceProviderProxy.Init(dns, zone.ServiceId);

                        DnsRecord[] domainRecords = dns.GetZoneRecords(zone.Name);
                        var duplicateRecords = (from zoneRecord in domainRecords
                                                from resRecord in resourceRecords
                                                where zoneRecord.RecordName == resRecord.RecordName
                                                where zoneRecord.RecordType == resRecord.RecordType
                                                select zoneRecord).ToArray();
                        if (duplicateRecords != null && duplicateRecords.Count() > 0)
                        {
                            dns.DeleteZoneRecords(zone.Name, duplicateRecords);
                        }

                        // add new resource records
                        dns.AddZoneRecords(zone.Name, resourceRecords.ToArray());
                    }
                    catch (Exception ex1)
                    {
                        TaskManager.WriteError(ex1, "Error updating DNS records");
                    }
                }

                // update host headers
                List<ServerBinding> bindings = new List<ServerBinding>();

                // get existing web site bindings
                WebServer web = new WebServer();
                ServiceProviderProxy.Init(web, siteItem.ServiceId);
                    
                bindings.AddRange(web.GetSiteBindings(siteItem.SiteId));

                // check if web site has dedicated IP assigned
                bool dedicatedIp = bindings.Exists(binding => { return String.IsNullOrEmpty(binding.Host) && binding.IP != "*"; });

                // update binding only for "shared" ip addresses
                // add new host headers
                string ipAddr = "*";
                if (ip != null)
                    ipAddr = !String.IsNullOrEmpty(ip.InternalIP) ? ip.InternalIP : ip.ExternalIP;

                // fill bindings
                FillWebServerBindings(bindings, dnsRecords, ipAddr, hostName, domain.DomainName, ignoreGlobalDNSRecords);

                //for logging purposes
                foreach (ServerBinding b in bindings)
                {
                    string header = string.Format("{0} {1} {2}", b.Host, b.IP, b.Port);
                    TaskManager.WriteParameter("Add Binding", header);
                }

                // update bindings
                if (updateWebSite)
                    web.UpdateSiteBindings(siteItem.SiteId, bindings.ToArray(), false);

                // update domain
                if (!rebuild)
                {
                    domain.WebSiteId = siteItemId;
                    domain.IsDomainPointer = true;
                    foreach (ServerBinding b in bindings)
                    {
                        //add new domain record
                        if (!string.IsNullOrEmpty(b.Host))
                        {
                            domain.DomainName = b.Host;

                            DomainInfo domainTmp = ServerController.GetDomain(domain.DomainName);
                            if (!((domainTmp != null) && (domainTmp.WebSiteId == siteItemId)))
                            {
                                int domainID = ServerController.AddDomain(domain, domain.IsInstantAlias, false);
                                domainTmp = ServerController.GetDomain(domainID);
                                if (domainTmp != null)
                                {
                                    domainTmp.WebSiteId = siteItemId;
                                    domainTmp.ZoneItemId = domain.ZoneItemId;
                                    domainTmp.DomainItemId = domainId;

                                    ServerController.UpdateDomain(domainTmp);
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (domain.ZoneItemId > 0)
                    {
                        DomainInfo domainTmp = ServerController.GetDomain(string.IsNullOrEmpty(hostName) ? domain.DomainName : hostName + "." + domain.DomainName, true, true);
                        if (domainTmp != null)
                        {
                            domainTmp.ZoneItemId = domain.ZoneItemId;
                            ServerController.UpdateDomain(domainTmp);
                        }
                    }
                }

                return 0;
            }
            catch (Exception ex)
            {
                throw TaskManager.WriteError(ex);
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }
        public static int DeleteWebSitePointer(int siteItemId, int domainId, bool updateWebSite)
        {
            // check account
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive);
            if (accountCheck < 0) return accountCheck;

            // load site item
            WebSite siteItem = (WebSite)PackageController.GetPackageItem(siteItemId);
            if (siteItem == null)
                return BusinessErrorCodes.ERROR_WEB_SITE_PACKAGE_ITEM_NOT_FOUND;

            // load domain item
            DomainInfo domain = ServerController.GetDomain(domainId);
            if (domain == null)
                return BusinessErrorCodes.ERROR_DOMAIN_PACKAGE_ITEM_NOT_FOUND;

            // load appropriate zone
            DnsZone zone = (DnsZone)PackageController.GetPackageItem(domain.ZoneItemId);
            //if (zone == null)
            //    return BusinessErrorCodes.ERROR_DNS_PACKAGE_ITEM_NOT_FOUND;

            // get zone records for the service
            List<GlobalDnsRecord> dnsRecords = ServerController.GetDnsRecordsByService(siteItem.ServiceId);

            // change DNS zone
            IPAddressInfo ip = ServerController.GetIPAddress(siteItem.SiteIPAddressId);
            if (ip == null)
                return BusinessErrorCodes.ERROR_WEB_SITE_IP_ADDRESS_NOT_SPECIFIED;

            List<DnsRecord> resourceRecords = DnsServerController.BuildDnsResourceRecords(
                dnsRecords, domain.DomainName, ip.ExternalIP);

            // place log record
            TaskManager.StartTask("WEB_SITE", "DELETE_POINTER", siteItem.Name);
            TaskManager.ItemId = siteItemId;
            TaskManager.WriteParameter("Domain pointer", domain.DomainName);

            try
            {
                if (zone != null)
                {
                    try
                    {
                        DNSServer dns = new DNSServer();
                        ServiceProviderProxy.Init(dns, zone.ServiceId);
                        dns.DeleteZoneRecords(zone.Name, resourceRecords.ToArray());
                    }
                    catch(Exception ex1)
                    {
                        TaskManager.WriteError(ex1, "Error deleting DNS records");
                    }
                }

                // load web settings
                StringDictionary webSettings = ServerController.GetServiceSettings(siteItem.ServiceId);
                int sharedIpId = Utils.ParseInt(webSettings["SharedIP"], 0);

                bool dedicatedIp = (siteItem.SiteIPAddressId != sharedIpId);

                if (updateWebSite && !dedicatedIp)
                {

                    // update host headers
                    WebServer web = new WebServer();
                    ServiceProviderProxy.Init(web, siteItem.ServiceId);

                    List<ServerBinding> bindings = new List<ServerBinding>();
                    bindings.AddRange(web.GetSiteBindings(siteItem.SiteId));

                    // remove host headers
                    List<ServerBinding> domainBindings = new List<ServerBinding>();
                    FillWebServerBindings(domainBindings, dnsRecords, "", domain.DomainName);

                    // fill to remove list
                    List<string> headersToRemove = new List<string>();
                    foreach (ServerBinding domainBinding in domainBindings)
                    {
                        headersToRemove.Add(domainBinding.Host);
                    }

                    int pos = 0;
                    while (pos < bindings.Count)
                    {
                        if (headersToRemove.Contains(bindings[pos].Host))
                        {
                            bindings.RemoveAt(pos);
                            continue;
                        }
                        else
                        {
                            pos++;
                        }
                    }

                    web.UpdateSiteBindings(siteItem.SiteId, bindings.ToArray());
                }

                // update domain
                domain.WebSiteId = 0;
                ServerController.UpdateDomain(domain);

                return 0;
            }
            catch (Exception ex)
            {
                throw TaskManager.WriteError(ex);
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }
        internal static int AddWebSitePointer(int siteItemId, int domainId, bool updateWebSite)
        {
            // check account
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive);
            if (accountCheck < 0) return accountCheck;

            // load site item
            WebSite siteItem = (WebSite)PackageController.GetPackageItem(siteItemId);
            if (siteItem == null)
                return BusinessErrorCodes.ERROR_WEB_SITE_PACKAGE_ITEM_NOT_FOUND;

            // load domain item
            DomainInfo domain = ServerController.GetDomain(domainId);
            if (domain == null)
                return BusinessErrorCodes.ERROR_DOMAIN_PACKAGE_ITEM_NOT_FOUND;

            // load appropriate zone
            DnsZone zone = (DnsZone)PackageController.GetPackageItem(domain.ZoneItemId);
            //if (zone == null)
            //    return BusinessErrorCodes.ERROR_DNS_PACKAGE_ITEM_NOT_FOUND;

            // get zone records for the service
            List<GlobalDnsRecord> dnsRecords = ServerController.GetDnsRecordsByService(siteItem.ServiceId);

            // change DNS zone
            IPAddressInfo ip = ServerController.GetIPAddress(siteItem.SiteIPAddressId);
            if (ip == null)
                return BusinessErrorCodes.ERROR_WEB_SITE_IP_ADDRESS_NOT_SPECIFIED;

            List<DnsRecord> resourceRecords = DnsServerController.BuildDnsResourceRecords(
                dnsRecords, domain.DomainName, ip.ExternalIP);

            // place log record
            TaskManager.StartTask("WEB_SITE", "ADD_POINTER", siteItem.Name);
            TaskManager.ItemId = siteItemId;
            TaskManager.WriteParameter("Domain pointer", domain.DomainName);

            try
            {

                if (zone != null)
                {
                    try
                    {
                        DNSServer dns = new DNSServer();
                        ServiceProviderProxy.Init(dns, zone.ServiceId);

						// add new resource records
                        dns.AddZoneRecords(zone.Name, resourceRecords.ToArray());
                    }
                    catch(Exception ex1)
                    {
                        TaskManager.WriteError(ex1, "Error updating DNS records");
                    }
                }

                // update host headers
                if (updateWebSite)
                {
                    // load web settings
                    StringDictionary webSettings = ServerController.GetServiceSettings(siteItem.ServiceId);
                    int sharedIpId = Utils.ParseInt(webSettings["SharedIP"], 0);

                    bool dedicatedIp = (siteItem.SiteIPAddressId != sharedIpId);

                    if (!dedicatedIp)
                    {
                        WebServer web = new WebServer();
                        ServiceProviderProxy.Init(web, siteItem.ServiceId);

                        List<ServerBinding> bindings = new List<ServerBinding>();
                        ServerBinding[] siteBindings = web.GetSiteBindings(siteItem.SiteId);
                        if(siteBindings != null)
                            bindings.AddRange(siteBindings);

                        // add new host headers
                        string ipAddr = ip.InternalIP;
                        if (ipAddr == null || ipAddr == "")
                            ipAddr = ip.ExternalIP;

                        // fill bindings
                        FillWebServerBindings(bindings, dnsRecords, ipAddr, domain.DomainName);

                        // update bindings
                        web.UpdateSiteBindings(siteItem.SiteId, bindings.ToArray());
                    }
                }

                // update domain
                domain.WebSiteId = siteItemId;
                ServerController.UpdateDomain(domain);

                return 0;
            }
            catch (Exception ex)
            {
                throw TaskManager.WriteError(ex);
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }