<commit_before>﻿namespace Stumps.Utility {

    using System;
    using System.Net.NetworkInformation;

    internal static class NetworkUtility {

        public const int MinimumPort = 7000;
        public const int MaximumPort = 10000;

        public static int FindRandomOpenPort() {

            var properties = IPGlobalProperties.GetIPGlobalProperties();
            var endpointList = properties.GetActiveTcpListeners();

            var usedPorts = new bool[NetworkUtility.MaximumPort - NetworkUtility.MinimumPort + 1];

            foreach ( var endpoint in endpointList ) {
                if ( endpoint.Port >= NetworkUtility.MinimumPort && endpoint.Port <= NetworkUtility.MaximumPort ) {
                    var port = endpoint.Port - NetworkUtility.MinimumPort;
                    usedPorts[port] = true;
                }
            }

            var rnd = new Random();

            var foundPort = -1;

            // Maximum 100 tries for sanity
            for ( int i = 0; i < 100; i++ ) {
                var portGuess = rnd.Next(usedPorts.Length - 1);

                if ( !usedPorts[portGuess] ) {
                    foundPort = portGuess + NetworkUtility.MinimumPort;
                    break;
                }
            }

            return foundPort;

        }

    }

}
<commit_msg>Fixed a potential bug where a seemingly random open port is actually in use.<commit_after>﻿namespace Stumps.Utility {

    using System;
    using System.Net.NetworkInformation;

    internal static class NetworkUtility {

        public const int MinimumPort = 7000;
        public const int MaximumPort = 10000;

        public static int FindRandomOpenPort() {

            var properties = IPGlobalProperties.GetIPGlobalProperties();
            var endpointList = properties.GetActiveTcpConnections();

            var usedPorts = new bool[NetworkUtility.MaximumPort - NetworkUtility.MinimumPort + 1];

            foreach ( var endpoint in endpointList ) {
                if ( endpoint.LocalEndPoint.Port >= NetworkUtility.MinimumPort && endpoint.LocalEndPoint.Port <= NetworkUtility.MaximumPort ) {
                    var port = endpoint.LocalEndPoint.Port - NetworkUtility.MinimumPort;
                    usedPorts[port] = true;
                }
            }

            var rnd = new Random();

            var foundPort = -1;

            // Maximum 100 tries for sanity
            for ( int i = 0; i < 100; i++ ) {
                var portGuess = rnd.Next(usedPorts.Length - 1);

                if ( !usedPorts[portGuess] ) {
                    foundPort = portGuess + NetworkUtility.MinimumPort;
                    break;
                }
            }

            return foundPort;

        }

    }

}

{'fraction_non_alphanumeric': 0.07784648963259368, 'fraction_numerical': 0.013823208439432522, 'mean_word_length': 1.6803118908382066, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '663954', 'n_tokens_mistral': 745, 'n_tokens_neox': 661, 'n_words': 179}