   while (WiFi.status() != WL_CONNECTED) {
     unsigned long now = millis();
     if (now - connectStart > 5000) {
-      unsigned long sleepMillis = 5000;
-      systemState.connectMillisCumulated += now - systemStart;
-
-      if (systemState.connectMillisCumulated >= connectInvainThreshold) {
-        systemState.connectMillisCumulated = 0;
-        sleepMillis = 45 * 60 * 1000L; // sleep long
-      }
-      
-      systemState.writeToRtc(0, sizeof(SystemState));
-
-      Serial.print("Waiting ");
-      Serial.println(sleepMillis);
-      
-      ESP.deepSleep(sleepMillis * 1000, RF_NO_CAL); // NOTE this must be taken plus the wait time above
+      sleepNowForFailedConnect(5000L, now - systemStart);
 
       /*
       // This has 15mA instead of 70mA (above) or 1mA with deep sleep

      connectStart = millis();
      WiFi.begin("Wettergunde", "3ApCo_rtz_ppopp");
      */
      
      // TODO differentiate between successful and unsuccessful sleep time
    }
    
    delay(200);


}

void sleepNowForFailedConnect(unsigned long sleepMillis, unsigned long millisSinceSystemStart)
{
  systemState.connectMillisCumulated += millisSinceSystemStart;

  if (systemState.connectMillisCumulated >= connectInvainThreshold) {
    systemState.connectMillisCumulated = 0;
    sleepMillis = 45 * 60 * 1000L; // sleep long
  }
  
  systemState.writeToRtc(0, sizeof(SystemState));

  Serial.print("Waiting ");
  Serial.println(sleepMillis);
  
  ESP.deepSleep(sleepMillis * 1000, RF_NO_CAL); // NOTE this must be taken plus the wait time above
}

void sleepNowForServer(short serverSecondsUntilOff, unsigned long systemOnMillis)
{
  unsigned long sleepMillis = 1;
