   # Creates the main object which will connects to Xen servers and
   # manages all the models.
   xo = new $XO()
-
-  # Starts it.
   xo.start {
     redis: {
       uri: opts.redis?.uri
     }
   }
 
-  $debug 'Initializing connection to Xen servers…'
-
-  # Static file serving (e.g. for XO-Web).
+  # Connect is used for managing non WebSocket connections.
   connect = $connect()
-  for urlPath, filePaths of opts.http.mounts
-    filePaths = [filePaths] unless $isArray filePaths
-    for filePath in filePaths
-      $debug 'Setting up %s → %s', urlPath, filePath
-      connect.use urlPath, $serveStatic filePath
-  webServer.on 'request', connect
-
-  # Creates the API.
-  api = new $API xo
-
+  webServer.on('request', connect)
+
+  # Must be set up before the API.
+  setUpConsoleProxy(webServer, xo)
+
+  # Must be set up before the API.
   connect.use $bind xo.handleProxyRequest, xo
 
-  # WebSocket server for consoles.
-  setUpConsoleProxy(webServer, xo)
-
-  # Create the WebSocket server.
-  wsServer = new $WSServer {
-    server: webServer
-    path: '/api/'
-  }
-  wsServer.on 'error', (error) ->
-    console.error '[WARN] WebSocket server', error
-    wsServer.close()
-    return
-
-  # Handle a WebSocket connection
-  wsServer.on 'connection', (socket) ->
-    $debug '+ WebSocket connection'
-
-    # Forward declaration due to cyclic dependency connection <-> jsonRpc.
-    connection = null
-
-    # Create a JSON-RPC interface for this connection.
-    jsonRpc = $jsonRpc.createServer (message) ->
-      return api.exec connection, message if message.type is 'request'
-
-    # Create a XO user connection.
-    connection = xo.createUserConnection {
-      close: $bind socket.close, socket
-      notify: $bind jsonRpc.notify, jsonRpc
-    }
-
-    # Close the connection with the socket.
-    socket.on 'close', ->
-      $debug '- WebSocket connection'
-      connection.close()
-      return
-
-    # Connect the WebSocket to the JSON-RPC server
-    socket.on 'message', (message) ->
-      jsonRpc.write message
-      return
-    jsonRpc.on 'data', (data) ->
-      if socket.readyState is socket.OPEN
-        socket.send (JSON.stringify data), (error) ->
-          if error
-            console.error '[WARN] WebSocket send', error
-            connection.close()
-          return
-      return
-
-    socket.on 'error', (error) ->
-      console.error '[WARN] WebSocket connection', error
-      connection.close()
-      return
-
-    return
+  # Must be set up before the static files.
+  setUpApi(webServer, xo)
+
+  setUpStaticFiles(connect, opts.http.mounts)
 
   # Creates a default user if there is none.
   unless $wait xo.users.exists()

  }

  # Loads config files.
  opts = $wait $appConf.load 'xo-server',
    defaults: opts
    ignoreUnknownFormats: true

  $debug 'Configuration loaded.'



#=====================================================================

loadConfiguration = () ->
  defaults = {
    http: {
      listen: [
        port: 80
