static void record_browser_callback(
    AvahiSRecordBrowser*rr,
    AvahiIfIndex interface,
    AvahiProtocol protocol,
    AvahiBrowserEvent event,
    AvahiRecord *record,
    AvahiLookupResultFlags flags,
    void* userdata) {

    AvahiSAddressResolver *r = userdata;

    assert(rr);
    assert(r);

    switch (event) {
        case AVAHI_BROWSER_NEW:
            assert(record);
            assert(record->key->type == AVAHI_DNS_TYPE_PTR);

            if (r->interface > 0 && interface != r->interface)
                return;

            if (r->protocol != AVAHI_PROTO_UNSPEC && protocol != r->protocol)
                return;

            if (r->interface <= 0)
                r->interface = interface;

            if (r->protocol == AVAHI_PROTO_UNSPEC)
                r->protocol = protocol;

            if (!r->ptr_record) {
                r->ptr_record = avahi_record_ref(record);
                r->flags = flags;

                finish(r, AVAHI_RESOLVER_FOUND);
            }
            break;

        case AVAHI_BROWSER_REMOVE:
            assert(record);
            assert(record->key->type == AVAHI_DNS_TYPE_PTR);

            if (r->ptr_record && avahi_record_equal_no_ttl(record, r->ptr_record)) {
                avahi_record_unref(r->ptr_record);
                r->ptr_record = NULL;
                r->flags = flags;

                /** Look for a replacement */
                avahi_s_record_browser_restart(r->record_browser);
                start_timeout(r);
            }

            break;

        case AVAHI_BROWSER_CACHE_EXHAUSTED:
        case AVAHI_BROWSER_ALL_FOR_NOW:
            break;

        case AVAHI_BROWSER_FAILURE:

            if (r->retry_with_multicast) {
                r->retry_with_multicast = 0;

                avahi_s_record_browser_free(r->record_browser);
                r->record_browser = avahi_s_record_browser_prepare(r->server, r->interface, r->protocol, r->key, AVAHI_LOOKUP_USE_MULTICAST, record_browser_callback, r);

                if (r->record_browser) {
                    avahi_s_record_browser_start_query(r->record_browser);
                    start_timeout(r);
                    break;
                }
            }

            r->flags = flags;
            finish(r, AVAHI_RESOLVER_FAILURE);
            break;
    }
}
static void record_browser_callback(
    AvahiSRecordBrowser*rr,
    AvahiIfIndex interface,
    AvahiProtocol protocol,
    AvahiBrowserEvent event,
    AvahiRecord *record,
    AvahiLookupResultFlags flags,
    void* userdata) {

    AvahiSAddressResolver *r = userdata;

    assert(rr);
    assert(r);

    switch (event) {
        case AVAHI_BROWSER_NEW:
            assert(record);
            assert(record->key->type == AVAHI_DNS_TYPE_PTR);

            if (r->interface > 0 && interface != r->interface)
                return;

            if (r->protocol != AVAHI_PROTO_UNSPEC && protocol != r->protocol)
                return;

            if (r->interface <= 0)
                r->interface = interface;

            if (r->protocol == AVAHI_PROTO_UNSPEC)
                r->protocol = protocol;

            if (!r->ptr_record) {
                r->ptr_record = avahi_record_ref(record);
                r->flags = flags;

                finish(r, AVAHI_RESOLVER_FOUND);
            }
            break;

        case AVAHI_BROWSER_REMOVE:
            assert(record);
            assert(record->key->type == AVAHI_DNS_TYPE_PTR);

            if (r->ptr_record && avahi_record_equal_no_ttl(record, r->ptr_record)) {
                avahi_record_unref(r->ptr_record);
                r->ptr_record = NULL;
                r->flags = flags;

                 
                avahi_s_record_browser_restart(r->record_browser);
                start_timeout(r);
            }

            break;

        case AVAHI_BROWSER_CACHE_EXHAUSTED:
        case AVAHI_BROWSER_ALL_FOR_NOW:
            break;

        case AVAHI_BROWSER_FAILURE:

            if (r->retry_with_multicast) {
                r->retry_with_multicast = 0;

                avahi_s_record_browser_free(r->record_browser);
                r->record_browser = avahi_s_record_browser_prepare(r->server, r->interface, r->protocol, r->key, AVAHI_LOOKUP_USE_MULTICAST, record_browser_callback, r);

                if (r->record_browser) {
                    avahi_s_record_browser_start_query(r->record_browser);
                    start_timeout(r);
                    break;
                }
            }

            r->flags = flags;
            finish(r, AVAHI_RESOLVER_FAILURE);
            break;
    }
}