public void Connect(string username, string password)
        {
            if (_TcpClient.Connected == false)
            {
                _TcpClient.Connect(HOST, PORT);

                Prompt prompt = new Prompt();
                prompt.status = Status.Connected;
                this.ShowPrompt(prompt);
            }
            if (_ServerStream == null)
            {
                _ServerStream = _TcpClient.GetStream();
            }

            UserAccount usr = new UserAccount();
            usr.username = string.IsNullOrEmpty(username) ? "Guest" : username;
            usr.email = "[email protected]";
            string jstr = JS.Serialize<UserAccount>(usr);

            this.SendRaw(jstr);

            Thread task = new Thread(() =>
            {
                _ServerStream = _TcpClient.GetStream();

                try
                {
                    byte[] bytes = new byte[BUFFER_SIZE];
                    int bytesRead = _ServerStream.Read(bytes, 0, bytes.Length);
                    UserAccount user = JS.Deserialize<UserAccount>(bytes, bytesRead);

                    if (this.IsLoggedIn(user))
                    {
                        this._User = user;

                        Prompt prompt = new Prompt();
                        prompt.status = Status.LoggedIn;
                        this.ShowPrompt(prompt);

                        startChatListen();
                    }
                }
                catch(Exception ex)
                {
                    Prompt prompt = new Prompt();
                    prompt.status = (int)Status.LoggingError;
                    prompt.description = ex.Message;
                    this.ShowPrompt(prompt);
                }
            });
            _Threads.Add(task.GetHashCode(), task);
            task.Start();
        }
        public void ShowPrompt(Prompt prompt)
        {
            string msg = prompt.description;
            switch (prompt.status)
            {
                case ClientService.Status.Connected:
                    msg = "Холбогдлоо.";
                    break;
                case ClientService.Status.LoggedIn:
                    msg = "Нэвтэрч орлоо.";
                    break;
                case ClientService.Status.LoggedOut:
                    msg = "Нэвтэрч гарлаа.";
                    break;
                case ClientService.Status.LoggingError:
                    msg = "Нэвтрэх үед алдаа гарлаа!";
                    break;
                case ClientService.Status.CannotPass:
                    msg = "Илгээхэд асуудал гарлаа!";
                    break;
                case ClientService.Status.CannotSend:
                    msg = "Илгээж чадсангүй!";
                    break;
                case ClientService.Status.CannotReceived:
                    msg = "Хүлээн авч чадсангүй!";
                    break;
                case ClientService.Status.Disconnected:
                    msg = "Холболт салсан!";
                    break;
                default:
                    return;
            }

            txtConversation.Text = txtConversation.Text
                + Environment.NewLine + " >> " + msg;
        }
        private Thread startChatListen()
        {
            Thread task = new Thread(() =>
            {
                while (true)
                {
                    if (!_TcpClient.Connected)
                    {
                        Prompt prompt = new Prompt();
                        prompt.status = (int)Status.Disconnected;
                        this.ShowPrompt(prompt);
                        break;
                    }

                    _ServerStream = _TcpClient.GetStream();

                    try
                    {
                        byte[] bytes = new byte[BUFFER_SIZE];
                        int bytesRead = _ServerStream.Read(bytes, 0, bytes.Length);
                        Message message = JS.Deserialize<Message>(bytes, bytesRead);

                        this.ShowMessage(message);
                    }
                    catch
                    {
                        Prompt prompt = new Prompt();
                        prompt.status = (int)Status.CannotReceived;
                        this.ShowPrompt(prompt);
                        break;
                    }

                    Thread.Sleep(500);
                }
            });
            _Threads.Add(task.GetHashCode(), task);
            task.Start();
            return task;
        }
 /*----*/
 public void ShowPrompt(Prompt prompt)
 {
     try
     {
         if (_Control is Control)
         {
             ((Control)_Control).Invoke((ThreadStart)(
                 () =>
                 {
                     _Control.ShowPrompt(prompt);
                 }));
         }
     }
     catch
     {
     }
 }
 public void SendRaw(string jstr)
 {
     if (_ServerStream != null && _TcpClient.Connected)
     {
         byte[] outBytes = Encoding.UTF8.GetBytes(jstr);
         _ServerStream.Write(outBytes, 0, outBytes.Length);
         _ServerStream.Flush();
     }
     else
     {
         Prompt prompt = new Prompt();
         prompt.status = Status.CannotPass;
         this.ShowPrompt(prompt);
     }
 }
        public void SendMessage(string msg, string to = null)
        {
            if (_ServerStream != null && _TcpClient.Connected && this.IsLoggedIn())
            {
                Message message = new Message();
                message.from = this._User.username;
                message.to = to;
                message.data = msg;
                string jstr = JS.Serialize<Message>(message);

                byte[] outBytes = Encoding.UTF8.GetBytes(jstr);
                _ServerStream.Write(outBytes, 0, outBytes.Length);
                _ServerStream.Flush();
            }
            else
            {
                Prompt prompt = new Prompt();
                prompt.status = Status.CannotSend;
                this.ShowPrompt(prompt);
            }
        }