/// <summary>
        /// Adds WMS layer to map
        /// </summary>
        /// <param name="msi">map service information</param>
        /// <param name="fromServerUrl">service url</param>
        /// <returns></returns>
        private bool addLayerWMS2(MapServiceInfo msi, Boolean fromServerUrl)
        {
            bool flag = false;
            if (msi == null) { throw new ArgumentNullException("msi"); }
            string service = msi.Service;
            string url = AppendQuestionOrAmpersandToUrlString(msi.Server);
            // append serviceParam to server url?
            if (msi.ServiceParam.Length > 0 && !fromServerUrl)
            {
                url = url + msi.ServiceParam;
                url = AppendQuestionOrAmpersandToUrlString(url);
            }


            CswClient client = new CswClient();
            string response = client.SubmitHttpRequest("GET", url + "request=GetCapabilities&service=WMS", "");
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(response);
            XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(xmlDoc.NameTable);
            xmlnsManager.AddNamespace("wms", "http://www.opengis.net/wms");
            XmlNodeList nl = null;
            if (xmlDoc.SelectSingleNode("//wms:Layer", xmlnsManager) != null)
            {
                nl = xmlDoc.SelectNodes("/wms:WMS_Capabilities/wms:Capability/wms:Layer/wms:Layer/wms:Title", xmlnsManager);

            }

            if (nl != null)
            {
                flag = true;
                for (int i = nl.Count - 1; i >= 0; i--)
                {

                    AGXD.ServiceConnectionProperties conn = new AGXD.ServiceConnectionProperties
                        (AGXD.ServiceType.Wms,
                        new Uri(url),
                        "", nl.Item(i).InnerText);

                    AGXM.ServiceLayer sl = new AGXM.ServiceLayer(conn);

                    bool connected = sl.Connect();

                    if (connected)
                    {
                        addLayer(sl);
                        addedLayer.Add(DateTime.Now.Millisecond, sl);
                    }
                }
            }
            return flag;
        }
        /// <summary>
        /// Function to add ArcIMS map serivce
        /// </summary>
        ///<param name="msi">The map service info</param>
        private void addLayerArcIMS(MapServiceInfo msi)
        {
            if (msi == null) { throw new ArgumentNullException("msi"); }
            //IIMSConnection imsConnection;
            try
            {
                AGXD.ServiceConnectionProperties sc = new AGXD.ServiceConnectionProperties();

                sc.ServiceType = (AGXD.ServiceType)Enum.Parse(typeof(AGXD.ServiceType), "Ims");
                AGXM.ServiceLayer resultLayer = new AGXM.ServiceLayer();
                sc.ServiceName = msi.Service;
                sc.SubServiceName = "";
                sc.Url = new System.Uri(msi.Server);
                //   resultLayer.Username = "";
                //  resultLayer.Password = "";
                //resultLayer.Name = "Layer";
                //   resultLayer.Copyright = "";
                resultLayer.Visible = true;
                resultLayer.ServiceConnectionProperties = sc;
                resultLayer.Connect();
                addLayer(resultLayer);
                addedLayer.Add(DateTime.Now, resultLayer);
            }
            catch (Exception ex)
            {
                throw new Exception(resourceManager.GetString("failedIMSService"), ex);
            }
            return;
        }
        /// <summary>
        /// Adds ArcGIS service endpoint to map
        /// </summary>
        /// <param name="fileName">service url or layer filename</param>
        private void AddAGSService(string fileName)
        {
            try
            {

                if (fileName.ToLower().Contains("http") && !fileName.ToLower().Contains("arcgis/rest"))
                {
                    if (fileName.EndsWith("MapServer"))
                        fileName = fileName.Remove(fileName.LastIndexOf("MapServer"));

                    if (fileName.EndsWith("GlobeServer"))
                    {
                        ShowErrorMessageBox("AddArcGISLayerFailed" + "\r\n" + "Adding Globe Server service not supported");
                        return;
                    }

                    String[] s = fileName.Split(new String[] { "/services" }, StringSplitOptions.RemoveEmptyEntries);
                    AGXD.ServiceConnectionProperties sc = new AGXD.ServiceConnectionProperties();
                    AGXM.ServiceLayer mapServerLayer = new AGXM.ServiceLayer();
                    sc.Url = new System.Uri(s[0] + "/services");
                    sc.ServiceType = AGXD.ServiceType.MapServer;
                    String[] s1 = null;
                    if (s.Length > 1)
                    {
                        s1 = s[1].Split('?');
                    }
                    else
                        return;

                    String[] s2 = null;
                    if (s1 != null)
                    {
                        s2 = s1[0].ToLower().Split(new String[] { "/mapserver" }, StringSplitOptions.RemoveEmptyEntries);
                    }
                    else
                        return;

                    String[] serviceName = null;

                    if (s2 != null)
                        serviceName = s2[0].Split(new String[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
                    else
                        return;

                    if (serviceName != null)
                        mapServerLayer.Name = serviceName[serviceName.Length - 1];
                    else
                        return;

                    sc.ServiceName = s2[0];
                    mapServerLayer.CachePolicy = AGXM.LayerCachePolicy.RemoveOnApplicationExit; // esriE2LayerCacheType.Session;

                    try
                    {
                        mapServerLayer.Connect();
                    }
                    catch (Exception e) { }

                    if (mapServerLayer.IsConnected)
                    {
                        addLayer(mapServerLayer);
                        addedLayer.Add(DateTime.Now, mapServerLayer);
                    }
                }
                else
                {
                    System.IO.StreamReader fStream = System.IO.File.OpenText(fileName);
                    string xml = fStream.ReadToEnd().Trim();
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(xml);

                    XmlNodeList layersNode = doc.GetElementsByTagName("E2Layers");
                    foreach (XmlNode xmlnode in layersNode)
                    {
                        XmlNodeList layerNodes = xmlnode.ChildNodes;
                        foreach (XmlNode node in layerNodes)
                        {
                            if (node.HasChildNodes && node.Attributes.Item(0).Value.Equals("esri:E2ServerLayer"))
                            {
                                AGXD.ServiceConnectionProperties sc = new AGXD.ServiceConnectionProperties();
                                AGXM.ServiceLayer svrLyr = new AGXM.ServiceLayer();
                                foreach (XmlNode childNode in node)
                                {
                                    if (childNode.Name.Equals("DisplayName"))
                                        svrLyr.Name = childNode.InnerText;
                                    else if (childNode.Name.Equals("ServerType"))
                                        sc.ServiceType = (AGXD.ServiceType)Int16.Parse(childNode.InnerText);
                                    else if (childNode.Name.Equals("Url"))
                                        sc.Url = new System.Uri(childNode.InnerText);
                                    else if (childNode.Name.Equals("ServiceName"))
                                        sc.ServiceName = childNode.InnerText;
                                    else if (childNode.Name.Equals("SubserviceName"))
                                        sc.SubServiceName = childNode.InnerText;
                                }
                                svrLyr.ServiceConnectionProperties = sc;
                                svrLyr.Connect();
                                if (svrLyr.IsConnected)
                                {
                                    addLayer(svrLyr);
                                    addedLayer.Add(DateTime.Now, svrLyr);
                                }

                            }
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                ShowErrorMessageBox("AddArcGISLayerFailed" + "\r\n" + ex.Message);
            }
        }
        /// <summary>
        /// Function to add WMS map serivce
        /// </summary>
        ///<param name="msi">The map service info</param>
        ///<param name="fromServerUrl">from server url</param>
        private void addLayerWMS(MapServiceInfo msi, Boolean fromServerUrl)
        {
            if (msi == null) { throw new ArgumentNullException("msi"); }
            string service = msi.Service;
            string url = AppendQuestionOrAmpersandToUrlString(msi.Server);
            // append serviceParam to server url?
            if (msi.ServiceParam.Length > 0 && !fromServerUrl)
            {
                url = url + msi.ServiceParam;
                url = AppendQuestionOrAmpersandToUrlString(url);
            }
            // connect to wms service
            try
            {
                AGXD.ServiceConnectionProperties sc = new AGXD.ServiceConnectionProperties();
                sc.ServiceType = (AGXD.ServiceType)Enum.Parse(typeof(AGXD.ServiceType), "Wms");
                AGXM.ServiceLayer resultLayer = new AGXM.ServiceLayer();
                if (msi.Service != null && msi.Service.Trim().Length > 0)
                    sc.ServiceName = msi.Service;
                else
                    sc.ServiceName = "Unknown Service Name";
                sc.SubServiceName = "";
                sc.Url = new System.Uri(url);
                //     sc.Username = "";
                //    sc.Password = "";           
                resultLayer.Visible = true;
                resultLayer.ServiceConnectionProperties = sc;

                resultLayer.Connect();
                addLayer(resultLayer);
                addedLayer.Add(DateTime.Now, resultLayer);

            }
            catch (Exception ex)
            {
                throw new Exception(resourceManager.GetString("failedWMSService"), ex);
            }

            return;
        }