bool OBPTransaction::sendCommandToDevice(TransferHelper *helper,
                    unsigned int messageType,
                    vector<byte> &data) throw (ProtocolException) {

    bool retval = false;
    int flag = 0;
    vector<byte> *bytes = NULL;
    OBPMessage *message = new OBPMessage();
    OBPMessage *response = NULL;

    message->setMessageType(messageType);
    message->setAckRequestedFlag();

    /* Need a copy of the input data that can be given to the message. */
    /* Note: copy will be deleted by message when appropriate. */
    message->setData(new vector<byte>(data));

    try {
        bytes = message->toByteStream();
        flag = helper->send(*bytes, (unsigned) bytes->size());
        if(((unsigned int)flag) != bytes->size()) {
            /* FIXME: retry, throw exception, something here */
        }
    } catch (BusException &be) {
        if(NULL != bytes) {
            delete bytes;
        }
        delete message;
        string error("Failed to write to bus.");
        /* FIXME: previous exception should probably be bundled up into the new exception */
        /* FIXME: there is probably a more descriptive type for this than ProtocolException */
        throw ProtocolException(error);
    }

    delete message;
    delete bytes;
    bytes = NULL;

    try {
        /* Read the 64-byte OBP header. */
        bytes = new vector<byte>(64);
        flag = helper->receive(*bytes, (unsigned) bytes->size());
        if(((unsigned int)flag) != bytes->size()) {
            /* FIXME: retry, throw exception, something here */
        }

        /* Parse out the header. */
        try {
            response = OBPMessage::parseHeaderFromByteStream(bytes);
        } catch (IllegalArgumentException &iae) {
            response = NULL;
        }
    } catch (BusException &be) {
        if(NULL != bytes) {
            delete bytes;
        }
        string error("Failed to read from bus.");
        /* FIXME: previous exception should probably be bundled up into the new exception */
        /* FIXME: there is probably a more descriptive type for this than ProtocolException */
        throw ProtocolException(error);
    }

    delete bytes;

    if(NULL == response || true == response->isNackFlagSet()
            || response->getMessageType() != messageType) {
        retval = false;
    } else if(true == response->isAckFlagSet()) {
        retval = true;
    } else {
        delete response;
        string error("Illegal device response");
        throw ProtocolException(error);
    }
    delete response;
    return retval;
}