﻿using System;
using System.Net;
using System.Net.Sockets;

namespace Abc.Zebus.Util
{
    internal static class TcpUtil
    {
        public static int GetRandomUnusedPort()
        {
            var listener = new TcpListener(IPAddress.Any, 0);
            listener.Start();
            var port = ((IPEndPoint)listener.LocalEndpoint).Port;
            listener.Stop();
            return port;
        }

        public static bool IsPortUnused(int port)
        {
            var listener = new TcpListener(IPAddress.Any, port);
            try
            {
                listener.Start();
            }
            catch (Exception)
            {
                return false;
            }
            listener.Stop();
            return true;
        }
    }
}
﻿using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;

namespace Abc.Zebus.Util
{
    internal static class TcpUtil
    {
        public static int GetRandomUnusedPort()
        {
            var listener = new TcpListener(IPAddress.Any, 0);
            listener.Start();
            var port = ((IPEndPoint)listener.LocalEndpoint).Port;
            listener.Stop();
            return port;
        }

        public static bool IsPortUnused(int port)
        {
            var ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
            var activeTcpListeners = ipGlobalProperties.GetActiveTcpListeners();
            return activeTcpListeners.All(endpoint => endpoint.Port != port);
        }
    }
}

Change IsPortUnused() implementation because of SO_REUSEPORT on Linux
Change IsPortUnused() implementation because of SO_REUSEPORT on Linux
