qmerge_start (struct qmerge **qm, const char servers[64],
              int flagrecursive, const char *q,
              const char qtype[2], const char localip[4], const char *control)
{
    int i = 0, r = 0;
    struct qmerge_key k;

    qmerge_free (qm);

    byte_zero (&k, sizeof (k));
    if (!qmerge_key_init (&k, q, qtype, control))
        return -1;

    for (i = 0; i < QMERGE_MAX; i++)
    {
        if (!inprogress[i].active)
            continue;
        if (!qmerge_key_equal (&k, &inprogress[i].key))
            continue;

        log_tx_piggyback (q, qtype, control);
        inprogress[i].active++;
        *qm = &inprogress[i];
        qmerge_key_free (&k);

        return 0;
    }

    for (i = 0; i < QMERGE_MAX; i++)
        if (!inprogress[i].active)
          break;
    if (i == QMERGE_MAX)
        return -1;

    log_tx (q, qtype, control, servers, 0);
    r = dns_transmit_start (&inprogress[i].dt, servers,
                            flagrecursive, q, qtype, localip);
    if (r == -1)
    {
        qmerge_key_free (&k);
        return -1;
    }

    inprogress[i].active++;
    inprogress[i].state = 0;
    qmerge_key_free (&inprogress[i].key);
    byte_copy (&inprogress[i].key, sizeof (k), &k);
    *qm = &inprogress[i];

    return 0;
}
qmerge_start (struct qmerge **qm, const char servers[64],
              int flagrecursive, const char *q,
              const char qtype[2], const char localip[4], const char *control)
{
    int i = 0, r = 0;
    struct qmerge_key k;

    qmerge_free (qm);

    byte_zero (&k, sizeof (k));
    if (!qmerge_key_init (&k, q, qtype, control))
        return -1;

    for (i = 0; i < QMERGE_MAX; i++)
    {
        if (!inprogress[i].active)
            continue;
        if (!qmerge_key_equal (&k, &inprogress[i].key))
            continue;

        log_tx_piggyback (q, qtype, control);
        inprogress[i].active++;
        *qm = &inprogress[i];
        qmerge_key_free (&k);

        return 0;
    }

    for (i = 0; i < QMERGE_MAX; i++)
        if (!inprogress[i].active)
          break;
    if (i == QMERGE_MAX)
        return -1;

    log_tx (q, qtype, control, servers, 0);
    r = dns_transmit_start (&inprogress[i].dt, servers,
                            flagrecursive, q, qtype, localip);
    if (r == -1)
    {
        qmerge_key_free (&k);
        return -1;
    }

    inprogress[i].active++;
    inprogress[i].state = 0;
    qmerge_key_free (&inprogress[i].key);
    byte_copy (&inprogress[i].key, sizeof (k), &k);
    *qm = &inprogress[i];

    return 0;
}