private void AcceptClient(IAsyncResult ar)
        {
            if (!(ar.AsyncState is Socket listener))
            {
                return;
            }

            try
            {
                var client  = listener.EndAccept(ar);
                var address = client?.RemoteEndPoint.ToString();

                if (address != null)
                {
                    Trace.TraceInformation("ModbusSlave: Client connected " + address);
                    _connectedMasters.Add(client);
                    Connected?.Invoke(address);
                    LastAccess = DateTime.Now;

                    var codec  = new ModbusTcpCodec();
                    var server = new ModbusServer(codec)
                    {
                        Address = Id
                    };
                    var host = new TcpServer(client, server)
                    {
                        IdleTimeout = 60
                    };
                    host.ServeCommand += ListenerServeCommand;
                    host.Start();
                    host.Disconnected += () => OnClientDisconnected(client, address);
                }

                listener.BeginAccept(AcceptClient, listener);
            }
            catch (ObjectDisposedException)
            {
                // listener terminated
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.Message);
            }
        }
        public ConnectedSlave(ModbusMaster master, Socket connection, byte id)
        {
            Master            = master;
            _socketConnection = connection;
            CommClient        = _socketConnection.GetClient();
            if (connection.RemoteEndPoint is IPEndPoint ipEndPoint)
            {
                Address = ipEndPoint.Address;
                Port    = (ushort)ipEndPoint.Port;
            }
            else
            {
                throw new NotSupportedException("ConnectedSlave without IP endpoint");
            }
            Id = id;
            var codec = new ModbusTcpCodec();

            _client = new ModbusClient(codec)
            {
                Address = Id
            };
            _failures = 0;
        }