private async Task AcceptClients(CancellationToken cancellationToken = default) { logger.LogInformation("Server accepting clients [endpoint={@Endpoint}]", listener.LocalEndpoint); listener.Start(); await using (cancellationToken.Register(() => listener.Stop())) { while (!cancellationToken.IsCancellationRequested) { try { var client = await listener.AcceptTcpClientAsync(); logger.LogDebug("Client connected [client={@Client}]", client.Client.RemoteEndPoint); var chatUser = await identityProvider.Identify(client, cancellationToken); var tcpTransport = new TcpChatTransport(client, messageProtocol); tracker.Run(await chatServer.AddClientAsync(chatUser, tcpTransport, cancellationToken), cancellationToken); } catch (InvalidOperationException) { cancellationToken.ThrowIfCancellationRequested(); throw; } catch (Exception error) { logger.LogError(error, "Caught generic error while accepting clients"); } } } }