   isMainDomain
   setSessionCookie
   checkAuthorizationBearerHeader
+  isTeamPage
 }

    if err or not client
      console.error err
      return next()
    isLoggedIn req, res, (err, state, account) ->
      if err
        res.status(500).send error_500()
        return console.error err

      # if user is not logged in and requests the root page, serve the content
      # of hubspot
      if not state and req.path is '/' and KONFIG.environment not in ['dev', 'sandbox']

        # pipe incoming request directly to hubspot and pipe the response from
        # hubspot as our response
        return req.pipe(request(KONFIG.hubspotPageURL)).pipe(res)

      else
        client.connection.delegate = account

        { params }              = req
        { loggedIn, loggedOut } = JGroup.render
        fn                      = if state then loggedIn else loggedOut
        options = { client, account, bongoModels, params, session }

        fn.kodingHome options, (err, subPage) ->
          return next()  if err
          serve subPage, res


isLoggedIn = (req, res, callback) ->

  res.header 'Content-type', 'text/html'
  res.send content

isTeamPage = (req) ->
  hostname = req?.headers?['x-host']
  return no  unless hostname
  for i, env of ['dev', 'sandbox', 'latest']
    if hostname.indexOf(env) is 0 # damn nodejs doesnt have startsWith
      return no

  subdomains = hostname.split('.')

  if subdomains.length < 3 # only koding.com has 2
    return no

  return yes

serveHome = (req, res, next) ->
  { JGroup } = bongoModels = koding.models
  { generateFakeClient }   = require './client'
