 			//Notice: You need to define a cross domain policy file at your remote server root document, or have a policy file server on the target. 
 			Security.allowDomain("*");
 //			Security.loadPolicyFile("http://www.lookbackon.com/crossdomain.xml");  
-			//
+			//event listeners
 			mqttSocket.addEventListener(Event.CONNECT, onConnect); //dispatched when the connection is established
 			mqttSocket.addEventListener(Event.CLOSE, onClose); //dispatched when the connection is closed
 			mqttSocket.addEventListener(IOErrorEvent.IO_ERROR, onError); //dispatched when an error occurs
 			mqttSocket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData); //dispatched when socket can be read
 			mqttSocket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecError); //dispatched when security gets in the way
-			//
+			//try to connect
 			mqttSocket.connect(MY_HOST, MY_PORT);
-			//MQTT byte array prepare.
-			//@see https://www.ibm.com/developerworks/mydeveloperworks/blogs/messaging/entry/write_your_own_mqtt_client_without_using_any_api_in_minutes1?lang=en
+			//for subscribe: subscribes to topics */
+
 			//for connection
 			this.connectMesage.writeByte(0x10); //Connect
 			this.connectMesage.writeByte(0x0C + 0x04); //Remaining Length

	//
	//--------------------------------------------------------------------------
	import com.godpaper.mqtt.as3.utils.UIDUtil;

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;

		//  Private methods
		//
		//--------------------------------------------------------------------------
		//
		private function onConnect(event:Event):void
		{
			trace(event);
//			mqttSocket.writeUTFBytes("GET / HTTP/1.1\n");
//			mqttSocket.writeUTFBytes("Host: hejp.co.uk\n");
//			mqttSocket.writeUTFBytes("\n");
//			All data values are in big-endian order: higher order bytes precede lower order bytes.
			trace("MQTT byte order:",this.mqttSocket.endian);
			if (this.mqttSocket.endian != Endian.BIG_ENDIAN)
			{
				throw new Error("Endian failed!");
			}
			trace("MQTT connectMesage.length:", this.connectMesage.length);
			this.mqttSocket.writeBytes(this.connectMesage, 0, this.connectMesage.length);
			this.mqttSocket.flush();
		}

		//
		private function onClose(event:Event):void
		{
			// Security error is thrown if this line is excluded
			trace(event);
			mqttSocket.writeBytes(this.disconnectMessage, 0, this.disconnectMessage.length);
			mqttSocket.close();
		}

		//
		private function onError(event:IOErrorEvent):void
		{
			trace("MQTT IO Error: " + event);
		}

		//
		private function onSecError(event:SecurityErrorEvent):void
		{
			trace("MQTT Security Error: " + event);
		}

		//
		private function onSocketData(event:ProgressEvent):void
		{
			trace("MQTT Socket received " + this.mqttSocket.bytesAvailable + " byte(s) of data:");
			// Loop over all of the received data, and only read a byte if there  is one available 
			var connack:Vector.<int> = new Vector.<int>();
			while (mqttSocket.bytesAvailable)
			{
				// Read a byte from the socket and display it  
				connack.push(mqttSocket.readByte());
//				data = mqttSocket.readInt();
//				trace(mqttSocket.readUTFBytes(mqttSocket.bytesAvailable).toString());
			}
//			To verify if the client has successfully connected let's receive the CONNACK from the server.
//			The connect return code is sent in the variable header of a CONNACK message.
//			This field defines a one byte unsigned return code. 
//			The meanings of the values, shown in the tables below, are specific to the message type. 
//			A return code of zero (0) usually indicates success.
			trace(connack.toString());
			if(connack[0] == 0x20 && connack[2] == 0x00) 
			{
				trace("MQTT connection success.");
				//Connected ... So let's publish
				this.mqttSocket.writeBytes(this.publishMessage,0,this.publishMessage.length);
				this.mqttSocket.flush();
			}else
			{
				throw new Error("MQTT connect failed!");
			}
		
		}

	}
