async Task<ServerQueryState> SyncServer(Server server) {
IPEndPoint endPoint = null;
while (true) {
var portInUse = false;
try {
if (endPoint == null)
endPoint = _useRangedEndPoints ? GetEndPointFromQueue() : null;
var state = await server.UpdateAsync(endPoint).ConfigureAwait(false);
if (state.Canceled)
State.IncrementCancelled();
return state;
} catch (SocketException e) {
if (e.SocketErrorCode == SocketError.AddressAlreadyInUse) {
this.Logger()
.FormattedWarnException(e, "Port in use: " + (endPoint == null ? -1 : endPoint.Port));
// replace used bad port with new port and retry, repeat until running out of ports
portInUse = true;
endPoint = GetNextEndPoint();
} else {
this.Logger().FormattedWarnException(e);
return null;
}
} catch (OperationCanceledException e) {
#if DEBUG
this.Logger().FormattedDebugException(e);
#endif
return null;
} catch (Exception e) {
this.Logger().FormattedWarnException(e);
return null;
} finally {
if (_useRangedEndPoints && endPoint != null && !portInUse)
ReleaseEndPoint(endPoint);
}
}
}