 #include <ESP8266WiFi.h>
+#include <lwip/ip.h> // for endian conversion htons, htonl, ...
 #include <WiFiUdp.h>
 
+//const char* ssid     = "tulix-mobile";
 const char* ssid     = "tulix-upstairs";
 const char* password = "vivalasalsa07";
 
-const char* DESTINATION_HOST = "ulno-work";
+const char * DESTINATION_HOST = "ulno-work";
+//const char* DESTINATION_HOST = "192.168.43.1";
+//const char* DESTINATION_HOST = "192.168.15.167"; // asus phone
+//const char* DESTINATION_HOST = "192.168.15.168"; // note 3 phone
+//const char* DESTINATION_HOST = "192.168.15.198"; // fire stick
 //const char* DESTINATION_HOST = "localhost";
 //const char* DESTINATION_HOST = "192.168.15.194";
 int DESTINATION_PORT = 19877;
 
+const char *MAGIC = "GNCT"; // magic identifier for Game Network Controller
+const int PROTOCOL_VERSION = 1;
 const int NUMBER_OF_BUTTONS = 256;
 const int NUMBER_OF_BUTTON_BYTES = (NUMBER_OF_BUTTONS + 7)/8;
 const int NUMBER_OF_AXIS = 16;
 const int NUMBER_OF_AXIS_BYTES = NUMBER_OF_AXIS * 2;
-byte message[NUMBER_OF_BUTTON_BYTES + NUMBER_OF_AXIS_BYTES];
+const int MAX_BUFFER_SIZE = 128;
+const int BUFFER_HEADER_SIZE = 16;
+byte message[MAX_BUFFER_SIZE];
 const int MAX_NUMBER_OF_BUTTONS = 16;
 const int BUTTON_THRES = 16; // how many times has an on be counted to assume this button is on
 //const int STATUS_LED = 2;

unsigned char buttonCode[MAX_NUMBER_OF_BUTTONS];
unsigned int buttonPin[MAX_NUMBER_OF_BUTTONS];
bool buttonPullup[MAX_NUMBER_OF_BUTTONS];
unsigned int buttonThresCounter[MAX_NUMBER_OF_BUTTONS];
int buttonsAllocated=0;
int frame = 0;
WiFiUDP udp;
IPAddress serverIP;

void initButton( int buttonIndex, unsigned char code, int pin, bool pullup ) {
  buttonCode[ buttonIndex ] = code;
  buttonPin[ buttonIndex] = pin;
  buttonPullup[ buttonIndex ] = pullup;
  //setButtonState(code,false); //done already
  if(pullup) pinMode(pin, INPUT_PULLUP);
  else pinMode(pin, INPUT); // needs to be manually pulled down  

  bool anyPressed = false;
  bool changed = false;
  for( int i=0; i<buttonsAllocated; i++ ) {
    bool lastState = buttonLastState[i];
    bool buttonPressed = checkButton(i);
    if( lastState != buttonPressed) {
      buttonLastState[i] = buttonPressed;
      changed = true;
    }
    anyPressed |= buttonPressed;
