// Update is called once per frame
        public virtual void Update()
        {
            CurrentState = clientState;
            switch (clientState)
            {
            case 0:
                if (enable)
                {
                    TCPClientTask            = Task.Run(ConnectToTCPServer);
                    CheckAliveFailCache      = 0;
                    CheckAliveDelayTimeCache = CheckAliveDelayTime;
                    clientState = 10;
                }
                break;

            case 10:
                if (Aclient != null)
                {
                    if (Aclient.Connected)
                    {
                        TCPClientTask = Task.Run(WaitingMessage);
                        clientState   = 20;
                    }
                }
                if (!enable && Aclient == null)
                {
                    clientState = 0;
                }
                break;

            case 20:
                if (CheckAliveDelayTimeCache <= 0)
                {
                    CheckAliveDelayTimeCache = CheckAliveDelayTime;
                    // check alive, if not
                    if (!TCPClientIsAlive.IsConnected(Aclient))
                    {
                        CheckAliveFailCache++;
                    }
                    else
                    {
                        CheckAliveFailCache = 0;
                    }
                }
                else
                {
                    CheckAliveDelayTimeCache -= Time.deltaTime;
                }

                if (CheckAliveFailCache >= CheckAliveAllowTotalFailTime || !enable)
                {
                    DisconnectFromTCPServer();
                    ClientThreadQuitDelayTimeCache = ClientThreadQuitDelayTime;
                    clientState = 30;
                }
                else
                {    // if connection alive then send message according to delay
                    if (timerCache > 0)
                    {
                        timerCache -= Time.deltaTime;
                        return;
                    }
                    else
                    {
                        timerCache = messageCycleSendingTime;
                    }
                    string msgToSend = NewMessageToSend();
                    // send message if there are any
                    if (msgToSend != null)
                    {
                        SendingMessage(msgToSend);
                    }
                }
                break;

            case 30:
                if (TCPClientTask.Status == TaskStatus.RanToCompletion)
                {
                    clientState = 0;
                }
                ClientThreadQuitDelayTimeCache -= Time.deltaTime;
                if (ClientThreadQuitDelayTimeCache <= 0)
                {
                    currentThread.Abort();
                    clientState = 40;
                }
                break;

            case 40:
                Debug.Log(TCPClientTask.Status);
                if (TCPClientTask.Status == TaskStatus.Faulted)
                {
                    clientState = 0;
                }
                break;
            }
        }
        private void MyTCPServer()
        {
            try
            {
                currentThread = Thread.CurrentThread;
                TcpListener Mylistener = new TcpListener(IPAddress.Any, port);
                Mylistener.Start();
                bool clientAlive = false;
                System.Timers.Timer aliveTimer = new System.Timers.Timer();
                aliveTimer.Interval = CheckAliveDelayTime * 1000;    //timer is using ms
                aliveTimer.Elapsed += AliveTimer;
                do
                {
                    using (TcpClient c = Mylistener.AcceptTcpClient())
                        using (NetworkStream netStream = c.GetStream())
                        {
                            CheckAliveFailCache = 0;
                            clientAlive         = true;
                            // start looping waitting for new message
                            do
                            {
                                // check if connection is alive
                                //clientAlive = TCPClientIsAlive.IsConnected(c);

                                if (!aliveTimer.Enabled)
                                {
                                    aliveTimer.Start();
                                }
                                if (AliveTimerReached)
                                {
                                    AliveTimerReached = false;
                                    // check alive, if not
                                    if (!TCPClientIsAlive.IsConnected(c))
                                    {
                                        CheckAliveFailCache++;
                                    }
                                    else
                                    {
                                        CheckAliveFailCache = 0;
                                    }
                                }

                                if (CheckAliveFailCache >= CheckAliveAllowTotalFailTime)
                                {
                                    clientAlive = false;
                                }

                                if (clientAlive && enable && netStream.DataAvailable)
                                {
                                    string       msgIn = new BinaryReader(netStream).ReadString();
                                    BinaryWriter write = new BinaryWriter(netStream);
                                    string       response;
                                    if (unpack.MessageIn(msgIn, out List <float> outNum, out List <string> outStr))
                                    {
                                        ReceivedMessage(outNum, outStr);
                                        response = pack.GetDoneMessage();
                                    }
                                    else
                                    {
                                        response = pack.GetNotMatchMessage();
                                    }
                                    write.Write(response);

                                    write.Flush();
                                }
                            } while (clientAlive && enable);
                            aliveTimer.Stop();
                        }
                } while (enable);
                aliveTimer.Dispose();
                Mylistener.Stop();
            }
            catch (Exception e)
            {
                if (e.Message.Length > 2)
                {
                    IfError(e.Message);
                }
            }
        }