public void Connect(EndPoint ep, IOArgs args = null)
        {
            if (Disposed)
            {
                throw new ObjectDisposedException("");
            }

            if (Connected)
            {
                throw new SocketException((int)SocketError.IsConnected);
            }

            IOArgs connectArgs = args == null?IOArgs.Alloc() : args;

            connectArgs.RemoteEndPoint = ep;
            connectArgs.Handler        = this;

            try
            {
                if (!_socket.ConnectAsync(connectArgs))
                {
                    SubmitIO(connectArgs);
                }
            }
            catch (Exception ex)
            {
                if (args == null)
                {
                    connectArgs.Dispose();
                }

                throw ex;
            }
        }
        public void Accept(IOArgs args = null)
        {
            if (Disposed)
            {
                throw new ObjectDisposedException("");
            }

            IOArgs acceptArgs = args = args == null?IOArgs.Alloc() : args;

            acceptArgs.Handler = this;

            try
            {
                if (!_socket.AcceptAsync(acceptArgs))
                {
                    SubmitIO(acceptArgs);
                }
            }
            catch (Exception ex)
            {
                if (args == null)
                {
                    acceptArgs.Dispose();
                }

                throw ex;
            }
        }
        public override void OnIOException(IOArgs args, Exception ex)
        {
            //Print the operation where args throw error:
            Console.WriteLine($"Exception { ex.Message }, { args.LastOperation }");

            //Dispose args, and dispose.
            args.Dispose();
            Dispose();
        }
 public void DoReceive(IOArgs args)
 {
     try
     {
         args.EnsureLength(2048); //Make sure we have space to receive on.
         args.SetBuffer(0, 2048); //Set range where want to receive.
         ReceiveArgs(args);       //Reuse same args for recv operation.
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex);
         args.Dispose(); //Dispose since error;
         Dispose();      //Dispose since exception.
     }
 }
        public void Send(string str)
        {
            IOArgs args = IOArgs.Alloc();

            args.SetBuffer(str);

            try
            {
                if (!_socket.SendAsync(args))
                {
                    SubmitIO(args);
                }
            }
            catch (Exception ex)
            {
                args.Dispose();

                throw ex;
            }
        }
        public void Disconnect(IOArgs args = null)
        {
            if (Disposed)
            {
                throw new ObjectDisposedException("");
            }

            if (!Connected)
            {
                throw new SocketException((int)SocketError.NotConnected);
            }

            _socket.Shutdown(SocketShutdown.Both);

            IOArgs diconnectArgs = args == null?IOArgs.Alloc(this) : args;

            diconnectArgs.DisconnectReuseSocket = true;
            diconnectArgs.Handler = this;

            try
            {
                if (!_socket.DisconnectAsync(diconnectArgs))
                {
                    SubmitIO(diconnectArgs);
                }
            }
            catch (Exception ex)
            {
                if (args == null)
                {
                    diconnectArgs.Dispose();
                }

                throw ex;
            }
        }
 public override void OnIOException(IOArgs args, Exception ex)
 {
     args.Dispose();
     Dispose();
 }