/* ==============================
        ** =    InitServer() Function   =
        ** ==============================
        **
        ** Initializes the server instance and starts it.
        ** Sets IP address and port and checks if port is valid.
        **
        ** Return values:
        ** ==============
        ** 0 -> Success.
        **
        ** 1 -> If the port is not larger than 1024 the function returns 1,
        ** indicating that the port in within the well-known port range
        ** and thus invalid for use of internal applications.
        **
        ** 2 -> The port is already beeing used by a other application or
        ** server instance and cannot be used.
        **
        ** 3 -> Sanity check indicating that still, after all checks the
        ** IP or port are invalid.
        **
        ** 5 + SocketError -> TcpListener could not be started.
        ** The return value is 5 + error code of the caught socket exception.
        ** To get the underlying socket error simply subtract 5 from
        ** the return value of this function.
        ** The SocketError enum defines SocketError.SocketError as -1.
        ** It symbolizes an unkown socket error, due to this value you
        ** need to check for 4 <= InitServer(IPAddress, int).
        **
        ** Example:
        ** ========
        **  int retVal = InitServer(IPAddress, int);
        **  if (4 <= retVal) {
        **      SocketError sErr = retVal - 5;
        **      throw new SocketException(sErr);
        **  }
        */
        private int InitServer(IPAddress address, int port)
        {
            if (1024 >= port)
            {
                return(1);
            } /* Check if port is within reserved and well-known range. */

            if (!(PortUtil.IsAvailable(port)))
            {
                return(2);
            } /* Check if the port is already used on the machine. */

            // Set IP address and port member.
            IPAddress = address;
            Port      = port;

            try {
                _listener = new TcpListener(IPAddress, Port);
                _listener.Start();
            } /* try to initializes internal TcpListener and start it. */
            catch (ArgumentNullException) {
                return(3);
            } /* IP or port where invalid. */
            catch (ArgumentOutOfRangeException) {
                return(3);
            } /* IP or port where out of range. */
            catch (SocketException ex) {
                return(5 + ex.ErrorCode);
            } /* TcpListener could not be started.*/

            // Everything went well.
            return(0);
        }
        /// <summary>
        /// Creates a new server instance on local host 127.0.0.1
        /// and a port between 46337 and 46997.
        /// </summary>
        /// <returns>Returns a server instance that runs
        /// on the local host address 127.0.0.1</returns>
        /// <exception cref="Exception">Throws any exception produced
        /// by the server(string, int) constructor.</exception>
        public static TinyTcpServer StartLocal()
        {
            try
            {
                int[] port = PortUtil.GetAvailablePorts(
                    PortRange.PR_46337_46997,
                    true);

                var server = new TinyTcpServer("127.0.0.1", port[0]);
                return(server);
            }
            catch (Exception e) {
                throw new Exception(e.Message);
            }
        }