/// <inheritdoc />
        public async Task EnableSslStreamAsync(X509Certificate certificate, CancellationToken cancellationToken)
        {
            await StopAsync(cancellationToken)
            .ConfigureAwait(false);

            try
            {
                _activeCommunicationService = new SslStreamConnectionAdapter(
                    _socketPipe,
                    _connectionPipe,
                    _sslStreamWrapperFactory,
                    certificate,
                    _connectionClosed,
                    _loggerFactory);
            }
            catch
            {
                _activeCommunicationService = new PassThroughConnectionAdapter(
                    _socketPipe,
                    _connectionPipe,
                    _connectionClosed,
                    _loggerFactory);
                throw;
            }
            finally
            {
                await StartAsync(cancellationToken)
                .ConfigureAwait(false);
            }
        }
        /// <inheritdoc />
        public async Task ResetAsync(CancellationToken cancellationToken)
        {
            await StopAsync(cancellationToken)
            .ConfigureAwait(false);

            _activeCommunicationService = new PassThroughConnectionAdapter(
                _socketPipe,
                _connectionPipe,
                _connectionClosed);
            await StartAsync(cancellationToken)
            .ConfigureAwait(false);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="SecureConnectionAdapter"/> class.
 /// </summary>
 /// <param name="socketPipe">The pipe from the socket.</param>
 /// <param name="connectionPipe">The pipe to the connection object.</param>
 /// <param name="sslStreamWrapperFactory">The SSL stream wrapper factory.</param>
 /// <param name="connectionClosed">The cancellation token for a closed connection.</param>
 public SecureConnectionAdapter(
     [NotNull] IDuplexPipe socketPipe,
     [NotNull] IDuplexPipe connectionPipe,
     [NotNull] ISslStreamWrapperFactory sslStreamWrapperFactory,
     CancellationToken connectionClosed)
 {
     _socketPipe                 = socketPipe;
     _connectionPipe             = connectionPipe;
     _sslStreamWrapperFactory    = sslStreamWrapperFactory;
     _connectionClosed           = connectionClosed;
     _activeCommunicationService = new PassThroughConnectionAdapter(
         socketPipe,
         connectionPipe,
         connectionClosed);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="SecureConnectionAdapter"/> class.
 /// </summary>
 /// <param name="socketPipe">The pipe from the socket.</param>
 /// <param name="connectionPipe">The pipe to the connection object.</param>
 /// <param name="sslStreamWrapperFactory">The SSL stream wrapper factory.</param>
 /// <param name="connectionClosed">The cancellation token for a closed connection.</param>
 /// <param name="loggerFactory">The logger factory.</param>
 public SecureConnectionAdapter(
     IDuplexPipe socketPipe,
     IDuplexPipe connectionPipe,
     ISslStreamWrapperFactory sslStreamWrapperFactory,
     CancellationToken connectionClosed,
     ILoggerFactory?loggerFactory = null)
 {
     _socketPipe                 = socketPipe;
     _connectionPipe             = connectionPipe;
     _sslStreamWrapperFactory    = sslStreamWrapperFactory;
     _connectionClosed           = connectionClosed;
     _loggerFactory              = loggerFactory;
     _activeCommunicationService = new PassThroughConnectionAdapter(
         socketPipe,
         connectionPipe,
         connectionClosed,
         loggerFactory);
 }