public NetListener(NetOrchestrator orchestrator, PrimaryConnectionEvent primaryConnectionHandler)
        {
            Orchestrator = orchestrator ?? throw new ArgumentNullException(nameof(orchestrator));

            PrimaryConnectionHandler = primaryConnectionHandler ??
                                       throw new ArgumentNullException(nameof(primaryConnectionHandler));

            Socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
        }
        public NetOrchestratorWorker(NetOrchestrator orchestrator)
        {
            Orchestrator = orchestrator ?? throw new ArgumentNullException(nameof(orchestrator));

            _packetWriteBuffer       = Orchestrator.Codec.MemoryManager.GetStream();
            _packetCompressionBuffer = Orchestrator.Codec.MemoryManager.GetStream();
            _flushRequestEvent       = new AutoResetEvent(false);

            Thread = new Thread(ThreadRunner);
        }
        public NetManager(RecyclableMemoryManager memoryManager)
        {
            MemoryManager = memoryManager ?? throw new ArgumentNullException(nameof(memoryManager));

            Codec        = new NetPacketCodec(MemoryManager);
            Orchestrator = new NetOrchestrator(MemoryManager, Codec);
            Listener     = new NetListener(Orchestrator, AcceptConnection);

            _connections = new HashSet <NetConnection>();
            Connections  = _connections.AsReadOnly();
        }
        public NetConnection(
            NetOrchestrator orchestrator,
            Socket socket,
            Action <NetConnection> closeAction)
        {
            Orchestrator = orchestrator ?? throw new ArgumentNullException(nameof(orchestrator));
            Socket       = socket ?? throw new ArgumentNullException(nameof(socket));
            _closeAction = closeAction ?? throw new ArgumentNullException(nameof(closeAction));

            // get it here as we can't get it later if the socket gets disposed
            RemoteEndPoint = (IPEndPoint)socket.RemoteEndPoint;

            ReceiveBuffer       = Orchestrator.MemoryManager.GetStream();
            DecompressionBuffer = Orchestrator.MemoryManager.GetStream();
            SendBuffer          = Orchestrator.MemoryManager.GetStream();

            ProtocolState = ProtocolState.Handshaking;
        }