Hey! Here's a quick take on a more idiomatic way for this. We basically get rid of the var's and use a `tailrec` method to walk through the ports and keep track of the attempts. We also collapse the 3 special cases down to 1:

```scala
  def startSprayServer(
    actorSystem: ActorSystem,
    ip: String,
    port: Int,
    route: Route,
    name: String = "HttpServer"): ActorRef = {
    val ioWorker = new IoWorker(actorSystem).start()
    val httpService = actorSystem.actorOf(Props(new HttpService(route)))
    val rootService = actorSystem.actorOf(Props(new SprayCanRootService(httpService)))

    @tailrec
    def connect(attempt: Int = 0, currentPort: Int = port): ActorRef = {
      // We may try to bind the actor multiple times if there is port
      // contention. To avoid naming conflicts, we have to assign
      // unique names for each attempt.
      val actorName = "%s_%s".format(name, attempt)
      val server = actorSystem.actorOf(
        Props(new HttpServer(ioWorker, SingletonHandler(rootService))), name = actorName)
      actorSystem.registerOnTermination { ioWorker.stop() }
      val timeout = 2.seconds
      val future = server.ask(HttpServer.Bind(ip, currentPort))(timeout)
      Try(Await.result(future, timeout)) match {
        case Success(_: HttpServer.Bound) => server
        case Failure(other) =>
          actorSystem.stop(server)
          logWarning("Failed to bind web UI to port %d: %s".format(currentPort, other))
          connect(attempt + 1, (currentPort + 1) % 65535)
      }
    }

    val boundActor = connect()
    logInfo("Created %s webUI at port %s".format(name, boundActor))
    boundActor
  }
```