public void Disconnect()
 {
     if (wsClient != null)
     {
         try
         {
             wsClient.Close();
         }
         catch
         {
         }
         finally
         {
             unsubscribeChannelEvents();
             wsClient = null;
         }
     }
 }
        public bool Connect(string url, string userName, out string errorMessage, out bool invalidUserName)
        {
            errorMessage    = "";
            invalidUserName = false;
            this.userName   = userName;
            if (wsClient == null)
            {
                wsClient = WebServiceUtils.CreateServiceClient(url);
                subscribeChannelEvents();

                try
                {
                    // the API call forces the channel open and also validates the user token
                    string text = "PING!";
                    if (wsClient.Echo(accessToken(), text) != text)
                    {
                        errorMessage = "Echo test failed.  Reflected text did not match original.";
                        return(false);
                    }
                }
                catch (Exception ex)
                {
                    InvalidUserException iuex = TranslateException(ex) as InvalidUserException;
                    if (iuex != null)
                    {
                        // echo failed because of invalid user
                        invalidUserName = true;
                        errorMessage    = iuex.Message;
                        return(false);
                    }
                    else
                    {
                        // echo failed for some other reason
                        errorMessage = string.Format("Connect Failed\r\n{0}\r\n{1}", ex.Message, ex.StackTrace);
                        return(false);
                    }
                }
            }
            return(true);
        }