@Override
	public byte[] readMessageAsBytes() throws IOException {
		InputStream is = socket.getInputStream();

		// read the length
		int bufLen;
		byte[] headerBuf = new byte[4];
		int len = is.read(headerBuf, 0, 4);

		int payloadLength = byteToInt(headerBuf);

		// use the smaller length between the "length" in the frame and the maxLength
		if (payloadLength < maxLength){
			bufLen = payloadLength;
		}else{
			bufLen = maxLength;
		}

		// read the payload
		byte[] buf = new byte[bufLen];
		if(bufLen == 0){
			// there's no payload
			return buf;
		} else { 
			// keep reading the payload and store it in the buf[]
			int counter = 0;
			try {
				while ( len >= 0 && counter < bufLen) {
					len = is.read(buf, counter, bufLen-counter);
					if (len > -1){
						counter += len;
					}
				}
			} catch (Exception e) {
				System.out.println("TCP read failed: " + e.getMessage());
			}
		}
		return buf;
	}