ConnectorBase
using Microsoft.Extensions.Logging;
using Renci.SshNet.Abstractions;
using Renci.SshNet.Common;
using Renci.SshNet.Messages.Transport;
using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace Renci.SshNet.Connection
{
    internal abstract class ConnectorBase : IConnector
    {
        private readonly ILogger _logger;
        internal ISocketFactory SocketFactory { get; set; }
        protected ConnectorBase(ISocketFactory socketFactory, ILoggerFactory loggerFactory)
        {
            ThrowHelper.ThrowIfNull(socketFactory, "socketFactory");
            SocketFactory = socketFactory;
            _logger = loggerFactory.CreateLogger(GetType());
        }
        public abstract Socket Connect(IConnectionInfo connectionInfo);
        public abstract Task<Socket> ConnectAsync(IConnectionInfo connectionInfo, CancellationToken cancellationToken);
        protected Socket SocketConnect(EndPoint endPoint, TimeSpan timeout)
        {
            _logger.LogInformation("Initiating connection to '{EndPoint}'.", endPoint);
            Socket socket = SocketFactory.Create(SocketType.Stream, ProtocolType.Tcp);
            try {
                SocketAbstraction.Connect(socket, endPoint, timeout);
                socket.SendBufferSize = 685360;
                socket.ReceiveBufferSize = 685360;
                return socket;
            } catch (Exception) {
                socket.Dispose();
                throw;
            }
        }
        [AsyncStateMachine(typeof(<SocketConnectAsync>d__9))]
        protected Task<Socket> SocketConnectAsync(EndPoint endPoint, CancellationToken cancellationToken)
        {
            <SocketConnectAsync>d__9 stateMachine = default(<SocketConnectAsync>d__9);
            stateMachine.<>t__builder = AsyncTaskMethodBuilder<Socket>.Create();
            stateMachine.<>4__this = this;
            stateMachine.endPoint = endPoint;
            stateMachine.cancellationToken = cancellationToken;
            stateMachine.<>1__state = -1;
            stateMachine.<>t__builder.Start(ref stateMachine);
            return stateMachine.<>t__builder.Task;
        }
        protected static byte SocketReadByte(Socket socket)
        {
            byte[] array = new byte[1];
            SocketRead(socket, array, 0, 1, Timeout.InfiniteTimeSpan);
            return array[0];
        }
        protected static byte SocketReadByte(Socket socket, TimeSpan readTimeout)
        {
            byte[] array = new byte[1];
            SocketRead(socket, array, 0, 1, readTimeout);
            return array[0];
        }
        protected static int SocketRead(Socket socket, byte[] buffer, int offset, int length)
        {
            return SocketRead(socket, buffer, offset, length, Timeout.InfiniteTimeSpan);
        }
        protected static int SocketRead(Socket socket, byte[] buffer, int offset, int length, TimeSpan readTimeout)
        {
            int num = SocketAbstraction.Read(socket, buffer, offset, length, readTimeout);
            if (num == 0)
                throw new SshConnectionException("An established connection was aborted by the server.", DisconnectReason.ConnectionLost);
            return num;
        }
    }
}