Move gateway selection helpers to backend.