@Override
  public org.openscada.hd.data.message.CreateQuery decodeMessage(
      final BinaryContext _context, final IoBuffer _data) throws Exception {
    // message code
    {
      final int messageCode = _data.getInt();

      if (messageCode != MESSAGE_CODE)
        throw new IllegalStateException(
            String.format("Expected messageCode %s but found %s", MESSAGE_CODE, messageCode));
    }

    final byte numberOfFields = _data.get();

    // decode attributes

    org.openscada.core.data.Request request = null;
    long queryId = 0L;
    String itemId = null;
    boolean updateData = false;
    org.openscada.hd.data.QueryParameters queryParameters = null;

    logger.trace("Decoding {} fields", numberOfFields);

    for (int i = 0; i < numberOfFields; i++) {

      final byte fieldNumber = _data.get();
      switch (fieldNumber) {
        case 1:
          {
            request =
                org.openscada.core.protocol.ngp.codec.Structures.decodeRequest(
                    _context, _data, false);
          }
          break;
        case 2:
          {
            queryId = _context.decodePrimitiveLong(_data);
          }
          break;
        case 3:
          {
            itemId = _context.decodeString(_data);
          }
          break;
        case 4:
          {
            updateData = _context.decodePrimitiveBoolean(_data);
          }
          break;
        case 5:
          {
            queryParameters =
                org.openscada.hd.protocol.ngp.codec.Structures.decodeQueryParameters(
                    _context, _data, false);
          }
          break;
        default:
          logger.warn("Received unknown field number: {}", fieldNumber);
          break;
      }
    }

    // create object
    return new org.openscada.hd.data.message.CreateQuery(
        request, queryId, itemId, updateData, queryParameters);
  }