Index: hci_comm_listener.c
===================================================================
--- hci_comm_listener.c	(revision 52)
+++ hci_comm_listener.c	(revision 53)
@@ -19,15 +19,20 @@
 int listenTo(unsigned short int portNum) {
 
     // Holders
-    char *attrVal = malloc(32*sizeof(char));
-    char *name = malloc(32*sizeof(char));
+    char *attrVal;
+    char *name;
 
 	// Buffer
 	char *requestBuffer = malloc(HCI_COMM_REQUEST_BUFFER_SIZE * sizeof(char));
 	if(requestBuffer == NULL) {
-		fprintf(stderr,"HCI comm error: Cannot allocate buffer.\n");
+		printf("HCI comm error: Cannot allocate buffer.\n");
 		return HCI_COMM_ERR_BUF_ALOC_FAIL;
 	}
+	char *responseBuffer = malloc(HCI_COMM_RESPONSE_BUFFER_SIZE * sizeof(char));
+	if(responseBuffer == NULL) {
+		printf("HCI comm error: Cannot allocate buffer.\n");
+		return HCI_COMM_ERR_BUF_ALOC_FAIL;
+	}
 
 	// Socket holder
 	int socketHolder = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
@@ -54,7 +59,7 @@
 	// Listen to port
 	int listenResult = listen(socketHolder, 10);
 	if(listenResult == -1) {
-		fprintf(stderr,"HCI comm error: Cannot listen to port %d.\n",portNum);
+		printf("HCI comm error: Cannot listen to port %d.\n",portNum);
 		close(socketHolder);
 		return HCI_COMM_ERR_LISTEN_FAIL;
 	}
@@ -62,18 +67,23 @@
 	// Infinite listening
 	while(1) {
 
+	    printf("[hci_comm_listener] Waiting for new connection...\n");
+
 		// Accept request
 		int connectionHolder = accept(socketHolder,NULL,NULL);
 		if(connectionHolder < 0) {
-			fprintf(stderr,"HCI comm error: Error accepting request.\n");
+			printf("HCI comm error: Error accepting request.\n");
 			close(socketHolder);
 			return HCI_COMM_ERR_ACCEPT_FAIL;
 		}
 
+		printf("[hci_comm_listener] New connection accepted\n");
+
 		// Read request
 		int readResult = read(connectionHolder,requestBuffer,HCI_COMM_REQUEST_BUFFER_SIZE);
+		printf("[hci_comm_listener] Recieved request: %s \n",requestBuffer);
 		if(readResult < 0) {
-			fprintf(stderr,"HCI comm error: Error reading request.\n");
+			printf("HCI comm error: Error reading request.\n");
 			close(socketHolder);
 			return HCI_COMM_ERR_READ_FAIL;
 		}
@@ -80,19 +90,37 @@
 
         // Parse request
         mxml_node_t *tree;
-        tree = mxmlLoadString(NULL, requestBuffer, MXML_TEXT_CALLBACK);
+        tree = mxmlLoadString(NULL, requestBuffer, MXML_NO_CALLBACK);
+        if(tree == NULL) {
+            printf("[hci_comm_listener] Error parsing request!\n");
+        }
+        printf("[hci_comm_listener] Request parsed\n");
 
 		// Create a response
-		char *hciResponse = "";
+		printf("[hci_comm_listener] Creating response\n");
 		int responseLength = 0;
 		mxml_node_t *node;
-        node = (tree, tree, "hci-request", "type", NULL, MXML_DESCEND);
-        strcpy(attrVal,node->value.element.attrs->value);
+        node = mxmlFindElement(tree, tree, "hci-request", NULL, NULL, MXML_DESCEND);
+        if(node == NULL) {
+            printf("[hci_comm_listener] Error: Recieved node is NULL!\n");
+        }
+        printf("[hci_comm_listener] Main request node found\n");
+        printf("[hci_comm_listener] %d\n",node);
+
+        attrVal = (char *)malloc(sizeof(node->value.element.attrs->value)*strlen(mxmlElementGetAttr(node, "type")));
+        attrVal = mxmlElementGetAttr(node,"type");
+        printf("[hci_comm_listener] Got type\n");
         if(strcmp(attrVal,"checkConnection") == 0) {
-            strcat(hciResponse,"<logic-response type=\"checkConnection\"><data>");
-            strcat(hciResponse,node->child->value.text.string);
-            strcat(hciResponse,"</data></logic-response>");
-            responseLength = strlen(hciResponse);
+            printf("[hci_comm_listener] checkConnection request\n");
+            strcpy(responseBuffer,"");
+            strcat(responseBuffer,"<logic-response type=\"checkConnection\"><data>");
+            mxml_node_t *child = node->child;
+            child = child->child;
+            strcat(responseBuffer,child->value.text.string);
+            strcat(responseBuffer,"</data></logic-response>");
+            printf("[hci_comm_listener] Response formed: %s\n",responseBuffer);
+            responseLength = strlen(responseBuffer);
+            printf("[hci_comm_listener] Response length formed: %d\n",responseLength);
         } else if(strcmp(attrVal,"getConfig") == 0) {
             // TODO
         } else if(strcmp(attrVal,"setConfig") == 0) {
@@ -104,7 +132,7 @@
         } else if(strcmp(attrVal,"executeAction") == 0) {
             // TODO
         } else {
-            strcpy(hciResponse,"<logic-response type=\"error\">Invalid request!</logic-response>");
+            strcpy(responseBuffer,"<logic-response type=\"error\">Invalid request!</logic-response>");
             responseLength = 62;
         }
 
@@ -114,13 +142,21 @@
 		}
 
 		// Send response
-		int writeResult = write(connectionHolder,hciResponse,responseLength);
+		int writeResult = write(connectionHolder,responseBuffer,responseLength);
+		printf("[hci_comm_listener] Written response to client\n");
 		if(writeResult < 0) {
-			fprintf(stderr,"HCI comm error: Error writting response.\n");
+			printf("HCI comm error: Error writting response.\n");
 			close(socketHolder);
 			return HCI_COMM_ERR_WRITE_FAIL;
 		}
 
+		// Close connection
+		free(attrVal);
+		shutdown(connectionHolder,SHUT_RDWR);
+		close(connectionHolder);
+
+		printf("[hci_comm_listener] Connection closed\n");
+
 	}
 
 	// Terminate
