private void FindAPortAndStartListener() { var foundAPort = false; var exception = new SocketException((int)SocketError.AddressAlreadyInUse); for (var enumerator = _tcpPortProvider.GetEnumerator(); !foundAPort && enumerator.MoveNext();) { _listener = new TcpListener(LocalEndpoint.Address, enumerator.Current); try { _listener.Start(); foundAPort = true; } catch (SocketException e) { exception = e; } } if (!foundAPort) { Exceptions.Throw(exception, "Could not find a port to listen on", LOGGER); } LOGGER.Log(Level.Info, string.Format("Listening on {0}", _listener.LocalEndpoint.ToString())); }