 #include <WiFiClient.h>
 #include <WiFiServer.h>
 
-const char* ssid = "...";
-const char* password = "...";
+const char* ssid = "...";  // Put your WIFI SSID here.
+const char* password = "...";  // Put your WIFI password here.
 
 WiFiServer server(4998);  // Uses port 4998.
 WiFiClient client;
 
-uint16_t *codeArray;
+uint16_t *code_array;
 IRsend irsend(4);  // an IR emitter led is connected to GPIO pin 4
 
-void parseString(String str) {
-  int16_t nextIndex;
-  uint16_t codeLength = 1;
-  uint16_t currentIndex = 0;
-  nextIndex = str.indexOf(',');
+void sendGCString(String str) {
+  int16_t index;
+  uint16_t count;
 
-  // change to do/until and remove superfluous repetition below...
-  while (nextIndex != -1) {
-    if (codeLength > 1)
-      codeArray = reinterpret_cast<uint16_t*>(
-          realloc(codeArray, codeLength * sizeof(uint16_t)));
-    else
-      codeArray = reinterpret_cast<uint16_t*>(
-          malloc(codeLength * sizeof(uint16_t)));
+  // Find out how many items there are in the string.
+  index = -1;
+  count = 1;
+  do {
+    index = str.indexOf(',', index + 1);
+    count++;
+  } while (index != -1);
 
-    codeArray[codeLength - 1] = (uint16_t) (
-        str.substring(currentIndex, nextIndex).toInt());
+  // Now we know how many there are, allocate the memory to store them all.
+  code_array = reinterpret_cast<uint16_t*>(malloc(count * sizeof(uint16_t)));
+  // Check we malloc'ed successfully.
+  if (code_array == NULL) {  // malloc failed, so give up.
+    Serial.printf("\nCan't allocate %d bytes. (%d bytes free)\n",
+                  count * sizeof(uint16_t), ESP.getFreeHeap());
+    Serial.println("Giving up & forcing a reboot.");
+    ESP.restart();  // Reboot.
+    delay(500);  // Wait for the restart to happen.
+    return;  // Should never get here, but just in case.
+  }
 
-    codeLength++;
-    currentIndex = nextIndex + 1;
-    nextIndex = str.indexOf(',', currentIndex);
-  }
-  codeArray = reinterpret_cast<uint16_t*>(
-      realloc(codeArray, codeLength * sizeof(uint16_t)));
-  codeArray[codeLength - 1] = (uint16_t) (
-      str.substring(currentIndex, nextIndex).toInt());
+  // Now convert the strings to integers and place them in code_array.
+  count = 0;
+  uint16_t start_from = 0;
+  do {
+    index = str.indexOf(',', start_from);
+    code_array[count] = str.substring(start_from, index).toInt();
+    start_from = index + 1;
+    count++;
+  } while (index != -1);
 
-  irsend.sendGC(codeArray, codeLength);
+  irsend.sendGC(code_array, count);  // All done. Send it.
+  free(code_array);  // Free up the memory allocated.
 }
 
 void setup() {
   // initialize serial:
   Serial.begin(115200);
+  delay(100);
   Serial.println(" ");
   Serial.println("IR TCP Server");
 

/*
 * IRremoteESP8266: IRGCTCPServer - send Global Cache-formatted codes via TCP.
 * An IR emitter must be connected to GPIO pin 4.
 * Version 0.1 1 April, 2016
 * Copyright 2016 Hisham Khalifa, http://www.hishamkhalifa.com
 *
 * Example command - Samsung TV power toggle: 38000,1,1,170,170,20,63,20,63,20,63,20,20,20,20,20,20,20,20,20,20,20,63,20,63,20,63,20,20,20,20,20,20,20,20,20,20,20,20,20,63,20,20,20,20,20,20,20,20,20,20,20,20,20,63,20,20,20,63,20,63,20,63,20,63,20,63,20,63,20,1798\r\n
 */

#include <ESP8266WiFi.h>

  }

  if (client.available()) {
    String ir_code_str = client.readStringUntil('\r');  // Exclusive of \r
    client.readStringUntil('\n');  // Skip new line as well
    client.flush();
    sendGCString(ir_code_str);
  }
}
