public void Dispose()
 {
     if (state == 0)
     {
         return;
     }
     state = 0;
     try { wsstream.Close(); } catch (Exception) { }
     try { wsstream.Dispose(); } catch (Exception) { }
     try { client.Close(); } catch (Exception) { }
     try { stream.Close(); } catch (Exception) { }
     stream   = null;
     client   = null;
     wsstream = null;
     wsclient = null;
     --parent.totalConnectCounter;
     parent.UpdateInfo();
 }
            public bool StartEx(MeshMapper parent, Uri url, string certhash, int counter)
            {
                if (state != 0)
                {
                    return(false);
                }
                state         = 1;
                this.parent   = parent;
                this.url      = url;
                this.counter  = counter;
                this.certhash = certhash;
                if (client != null)
                {
                    this.stream = client.GetStream();
                }
                Uri proxyUri = null;

                ++parent.totalConnectCounter;
                parent.UpdateInfo();

                // Check if we need to use a HTTP proxy (Auto-proxy way)
                try
                {
                    RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
                    Object      x           = registryKey.GetValue("AutoConfigURL", null);
                    if ((x != null) && (x.GetType() == typeof(string)))
                    {
                        string proxyStr = GetProxyForUrlUsingPac("http" + ((url.Port == 80) ? "" : "s") + "://" + url.Host + ":" + url.Port, x.ToString());
                        if (proxyStr != null)
                        {
                            proxyUri = new Uri("http://" + proxyStr);
                        }
                    }
                }
                catch (Exception) { proxyUri = null; }

                // Check if we need to use a HTTP proxy (Normal way)
                if (proxyUri == null)
                {
                    var proxy = System.Net.HttpWebRequest.GetSystemWebProxy();
                    proxyUri = proxy.GetProxy(url);
                    if ((url.Host.ToLower() == proxyUri.Host.ToLower()) && (url.Port == proxyUri.Port))
                    {
                        proxyUri = null;
                    }
                }

                if (proxyUri != null)
                {
                    // Proxy in use
                    proxyInUse = true;
                    wsclient   = new TcpClient();
                    wsclient.BeginConnect(proxyUri.Host, proxyUri.Port, new AsyncCallback(OnConnectSink), this);
                }
                else
                {
                    // No proxy in use
                    proxyInUse = false;
                    wsclient   = new TcpClient();
                    wsclient.BeginConnect(url.Host, url.Port, new AsyncCallback(OnConnectSink), this);
                }
                return(true);
            }