 unsigned int portMulti = 2525;
 
 const char* serverAddress = "";
+bool hasIP = false;
+bool multicastServerIsStarted = false;
 bool isInitialized = false;
 WiFiClient client;
 
+
+void WiFiEvent(WiFiEvent_t event) {
+  Serial.println("");
+  Serial.printf("[WiFi-event] event: %d\n", event);
+
+  switch(event) {
+    case WIFI_EVENT_STAMODE_GOT_IP:
+      Serial.println("WiFi connected");
+      hasIP = true;
+      break;
+    case WIFI_EVENT_STAMODE_DISCONNECTED:
+      Serial.println("WiFi lost connection");
+      break;
+  }
+}
 void setup()
 {
-  // Open serial communications and wait for port to open:
   Serial.begin(115200);
-  /*while (!Serial) {
-    ; // wait for serial port to connect.
-  }*/
-
-  // setting up Station AP
-  WiFi.mode(WIFI_STA);
+
+  WiFi.disconnect(true);
+
+  delay(1000);
+  WiFi.onEvent(WiFiEvent);
   WiFi.begin(ssid, pass);
 
-  // Wait for connect to AP
-  Serial.print("[Connecting]");
-  Serial.print(ssid);
-  int tries=0;
-  while (WiFi.status() != WL_CONNECTED) {
+  Serial.println();
+  Serial.println();
+  Serial.println("Wait for WiFi... ");
+  Serial.println();
+}
+
+void loop()
+{
+  if (!hasIP) {
+    Serial.print(".");
     delay(500);
-    Serial.print(".");
-    tries++;
-    if (tries > 30){
-      break;
-    }
-  }
-  Serial.println();
-
-
+  } else if (isInitialized) {
+    handleLEDs();
+  } else if (serverAddress == "") { // has server data received
+    if (multicastServerIsStarted) {
+      startMulticastServer();
+    } else {
+      listenForServer();
+    }
+  } else {
+    if (!lightIsRegistered()) {
+      registerLight();
+    } else {
+      isInitialized = true;
+    }
+  }
+}
+
+void startMulticastServer() {
+  Serial.println("> startMulticastServer()");
+  
   printWifiStatus();
-
-  Serial.println("Connected to wifi");
-
+  
   if (!Udp.beginMulticast(WiFi.localIP(), ipMulti, portMulti)) {
-  //if (!Udp.begin(portMulti)) {
     Serial.print("failed to start UDP Server at port ");
+    delay(500);
   } else {
     Serial.print("Udp Multicast server started at : ");
     Serial.print(ipMulti);
     Serial.print(":");
     Serial.println(portMulti);
-  }
-}
-
-void loop()
-{
-  if (isInitialized) {
-    handleLEDs();
-  } else if (serverAddress == "") {
-    listenForServer();
-  } else {
-    if (!lightIsRegistered()) {
-      registerLight();
-    } else {
-      isInitialized = true;
-    }
+    multicastServerIsStarted = true;
   }
 }
 
 void handleLEDs() {
+  Serial.println("> handleLEDs()");
   
 }
 
 void registerLight() {
+  Serial.println("> registerLight()");
   
 }
 
 bool lightIsRegistered() {
+  Serial.println("> lightIsRegistered()");
+  
   bool result = false;
   
   Serial.println("trying to determine whether the light is already registered");


int status = WL_IDLE_STATUS;
const char* ssid = "matrix";  //  your network SSID (name)
const char* pass = "51whc3xt";       // your network password

byte packetBuffer[512]; //buffer to hold incoming and outgoing packets


      serverAddress = hostname;
    }
  } // end if
}

void printWifiStatus() {
