public int ClaimNext() { lock (this._ports) { if (!this._ports.Any()) { this._ports.Add(IPHelper.FindFreePort(this._lower, this._upper)); if (this._ports[0] < 0) { throw new Exception("Unable to find a free port between " + this._lower + " and " + this._upper); } return(this._ports[0]); } int nextPort = this._lower; while (nextPort < this._upper) { if (!this._ports.Contains(nextPort) && IPHelper.IsPortFree(nextPort)) { this._ports.Add(nextPort); return(nextPort); } nextPort = nextPort + 1; } throw new Exception("Unable to find a free port between 8500 and 9000"); } }