uint32_t ifnum;
 } mdns_reply_t;
 
+static uint32_t _mdns_generation = 0;
 DNSServiceRef _mdns_sdref;
 DNSServiceRef _mdns_old_sdref;
 
        uint32_t domainlen = 0;
        char *domain = NULL;
 
+       if ((data == NULL) || (datalen == 0)) return NULL;
+
        // i: index into input data
        // j: index into output string
        while (datalen-- > 0) {
        size_t ansmaxlen; // DNS packet buffer maximum length
        int type; // type of query: A, AAAA, PTR, SRV...
        uint16_t last_type; // last type received
+       uint32_t sd_gen;
        DNSServiceRef sd;
        DNSServiceFlags flags;
        DNSServiceErrorType error;
 
        ctx->type = type;
        ctx->sd = _mdns_sdref;
+       ctx->sd_gen = _mdns_generation;
        ctx->kq = kq;
        if (reply) {
                ctx->reply = reply;
        int complete = _mdns_query_is_complete(ctx);
        if (ctx == NULL) return complete;
 
-       if (ctx->sd != NULL) {
+       /* only dealloc this DNSServiceRef if the "main" _mdns_sdref has not been deallocated */
+       if (ctx->sd != NULL && ctx->sd_gen == _mdns_generation) {
                DNSServiceRefDeallocate(ctx->sd);
-               ctx->sd = NULL;
        }
+
+       ctx->sd = NULL;
+       ctx->sd_gen = 0;
        ctx->flags = 0;
        ctx->kq = -1;
 
        timeout = delta;
        _mdns_now(&start);
 
+       for (i = 0; i < 2; ++i) {
+               memset(&ctx[i], 0 , sizeof(mdns_query_context_t));
+       }
+
        // set up the kqueue
        kq = kqueue();
        EV_SET(&ev, 1, EVFILT_USER, EV_ADD | EV_CLEAR, 0, 0, 0);
 
                        if (_mdns_sdref == NULL) {
                                if (_mdns_old_sdref != NULL) {
+                                       _mdns_generation++;
                                        DNSServiceRefDeallocate(_mdns_old_sdref);
                                        _mdns_old_sdref = NULL;
                                }
                                err == kDNSServiceErr_ServiceNotRunning ||
                                err == kDNSServiceErr_BadReference) {
                                if (_mdns_sdref) {
+                                       _mdns_generation++;
                                        DNSServiceRefDeallocate(_mdns_sdref);
                                        _mdns_sdref = NULL;
                                }
                                if (_mdns_debug) printf(";; DNSServiceProcessResult status %d\n", err);
                                err = 0;
                                // re-initialize the shared connection
+                               _mdns_generation++;
                                DNSServiceRefDeallocate(_mdns_sdref);
                                _mdns_sdref = NULL;
                                initialize = 1;
 
 struct audata {
        struct opaque_auth      au_origcred;    /* original credentials */
        struct opaque_auth      au_shcred;      /* short hand cred */
+#ifdef __LP64__
+       uint32_t                        au_shfaults;    /* short hand cache faults */
+#else
        u_long                  au_shfaults;    /* short hand cache faults */
+#endif
        char                    au_marshed[MAX_AUTH_BYTES];
        u_int                   au_mpos;        /* xdr pos at end of marshed */
 };
 
                bcopy(hp->h_addr, (char *)&server_addr.sin_addr, hp->h_length);
                server_addr.sin_family = AF_INET;
                server_addr.sin_port =  0;
-               if ((crp->client = clntudp_create(&server_addr, (u_long)prognum,
-                   (u_long)versnum, timeout, &crp->socket)) == NULL)
+#ifdef __LP64__
+               if ((crp->client = clntudp_create(&server_addr, (uint32_t)prognum, (uint32_t)versnum, timeout, &crp->socket)) == NULL)
                        return ((int) rpc_createerr.cf_stat);
+#else
+               if ((crp->client = clntudp_create(&server_addr, (u_long)prognum, (u_long)versnum, timeout, &crp->socket)) == NULL)
+                       return ((int) rpc_createerr.cf_stat);
+#endif
                crp->valid = 1;
                crp->oldprognum = prognum;
                crp->oldversnum = versnum;
 
 CLIENT *
 clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz)
 #ifdef __LP64__
-struct sockaddr_in *raddr;
-uint32_t prog;
-uint32_t vers;
-int *sockp;
-uint32_t sendsz;
-uint32_t recvsz;
+       struct sockaddr_in *raddr;
+       uint32_t prog;
+       uint32_t vers;
+       int *sockp;
+       uint32_t sendsz;
+       uint32_t recvsz;
 #else
-struct sockaddr_in *raddr;
-u_long prog;
-u_long vers;
-register int *sockp;
-u_int sendsz;
-u_int recvsz;
+       struct sockaddr_in *raddr;
+       u_long prog;
+       u_long vers;
+       register int *sockp;
+       u_int sendsz;
+       u_int recvsz;
 #endif
 {
        return clnttcp_create_timeout(raddr, (uint32_t)prog, (uint32_t)vers, sockp, (uint32_t)sendsz, (uint32_t)recvsz, NULL, NULL);
 static enum clnt_stat
 clnttcp_call(h, proc, xdr_args, args_ptr, xdr_results, results_ptr, timeout)
        register CLIENT *h;
+#ifdef __LP64__
+       uint32_t proc;
+#else
        u_long proc;
+#endif
        xdrproc_t xdr_args;
        caddr_t args_ptr;
        xdrproc_t xdr_results;
        register struct ct_data *ct = (struct ct_data *) h->cl_private;
        register XDR *xdrs = &(ct->ct_xdrs);
        struct rpc_msg reply_msg;
+#ifdef __LP64__
+       uint32_t x_id;
+       uint32_t *msg_x_id = (uint32_t *)(ct->ct_mcall);        /* yuk */
+#else
        u_long x_id;
        u_long *msg_x_id = (u_long *)(ct->ct_mcall);    /* yuk */
+#endif
        register bool_t shipnow;
        int refreshes = 2;
 
 
 CLIENT *
 clntudp_bufcreate(raddr, program, version, wait, sockp, sendsz, recvsz)
 #ifdef __LP64__
-struct sockaddr_in *raddr;
-uint32_t program;
-uint32_t version;
-struct timeval wait;
-int *sockp;
-uint32_t sendsz;
-uint32_t recvsz;
+       struct sockaddr_in *raddr;
+       uint32_t program;
+       uint32_t version;
+       struct timeval wait;
+       int *sockp;
+       uint32_t sendsz;
+       uint32_t recvsz;
 #else
-struct sockaddr_in *raddr;
-u_long program;
-u_long version;
-struct timeval wait;
-register int *sockp;
-u_int sendsz;
-u_int recvsz;
+       struct sockaddr_in *raddr;
+       u_long program;
+       u_long version;
+       struct timeval wait;
+       register int *sockp;
+       u_int sendsz;
+       u_int recvsz;
 #endif
 {
        return clntudp_bufcreate_timeout(raddr, (uint32_t)program, (uint32_t)version, sockp, (uint32_t)sendsz, (uint32_t)recvsz, &wait, NULL);
 static enum clnt_stat 
 clntudp_call(cl, proc, xargs, argsp, xresults, resultsp, utimeout)
        register CLIENT *cl;            /* client handle */
+#ifdef __LP64__
+       uint32_t                proc;           /* procedure number */
+#else
        u_long          proc;           /* procedure number */
+#endif
        xdrproc_t       xargs;          /* xdr routine for args */
        caddr_t         argsp;          /* pointer to args */
        xdrproc_t       xresults;       /* xdr routine for results */
                        cu->cu_error.re_errno = errno;
                        return (cu->cu_error.re_status = RPC_CANTRECV);
                }
-               if (inlen < sizeof(u_long))
-                       continue;       
-               /* see if reply transaction id matches sent id */
 #ifdef __LP64__
+               if (inlen < sizeof(uint32_t))
+                       continue;
+               /* see if reply transaction id matches sent id */
                if (*((uint32_t *)(cu->cu_inbuf)) != *((uint32_t *)(cu->cu_outbuf))) continue;
 #else
+               if (inlen < sizeof(u_long))
+                       continue;
+               /* see if reply transaction id matches sent id */
                if (*((u_long *)(cu->cu_inbuf)) != *((u_long *)(cu->cu_outbuf))) continue;
 #endif
                /* we now assume we have the proper reply */
 
 u_short
 pmap_getport(address, program, version, protocol)
 #ifdef __LP64__
-struct sockaddr_in *address;
-uint32_t program;
-uint32_t version;
-uint32_t protocol;
+       struct sockaddr_in *address;
+       uint32_t program;
+       uint32_t version;
+       uint32_t protocol;
 #else
-struct sockaddr_in *address;
-u_long program;
-u_long version;
-u_int protocol;
+       struct sockaddr_in *address;
+       u_long program;
+       u_long version;
+       u_int protocol;
 #endif
 {
        return pmap_getport_timeout(address, (uint32_t)program, (uint32_t)version, (uint32_t)protocol, NULL, NULL);
 
                if (! (*(cap->xdr_args))(xdrs, cap->args_ptr))
                    return (FALSE);
                position = XDR_GETPOS(xdrs);
+#ifdef __LP64__
+               cap->arglen = (uint32_t)position - (uint32_t)argposition;
+#else
                cap->arglen = (u_long)position - (u_long)argposition;
+#endif
                XDR_SETPOS(xdrs, lenposition);
                if (! xdr_u_long(xdrs, &(cap->arglen)))
                    return (FALSE);
        caddr_t port_ptr;
 
        port_ptr = (caddr_t)crp->port_ptr;
-       if (xdr_reference(xdrs, &port_ptr, sizeof (u_long), (xdrproc_t)xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
 #ifdef __LP64__
+       if (xdr_reference(xdrs, &port_ptr, sizeof (uint32_t), (xdrproc_t)xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
                crp->port_ptr = (unsigned int *)port_ptr;
+               return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
+       }
 #else
+       if (xdr_reference(xdrs, &port_ptr, sizeof (u_long), (xdrproc_t)xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) {
                crp->port_ptr = (unsigned long *)port_ptr;
-#endif
                return ((*(crp->xdr_results))(xdrs, crp->results_ptr));
        }
+#endif
        return (FALSE);
 }
 
                        stat = RPC_CANTRECV;
                        goto done_broad;
                }
+#ifdef __LP64__
+               if (inlen < sizeof(uint32_t))
+                       goto recv_again;
+#else
                if (inlen < sizeof(u_long))
                        goto recv_again;
+#endif
                /*
                 * see if reply transaction id matches sent id.
                 * If so, decode the results.
 
  */
 static struct svc_callout {
        struct svc_callout *sc_next;
+#ifdef __LP64__
+       uint32_t                    sc_prog;
+       uint32_t                    sc_vers;
+#else
        u_long              sc_prog;
        u_long              sc_vers;
+#endif
        void                (*sc_dispatch)();
 } *svc_head;
 
  */
 static struct svc_callout *
 svc_find(prog, vers, prev)
+#ifdef __LP64__
+       uint32_t prog;
+       uint32_t vers;
+#else
        u_long prog;
        u_long vers;
+#endif
        struct svc_callout **prev;
 {
        register struct svc_callout *s, *p;
        enum xprt_stat stat;
        struct rpc_msg msg;
        int prog_found;
+#ifdef __LP64__
+       uint32_t low_vers;
+       uint32_t high_vers;
+#else
        u_long low_vers;
        u_long high_vers;
+#endif
        struct svc_req r;
        register SVCXPRT *xprt;
+#ifdef __LP64__
+       register uint32_t mask;
+#else
        register u_long mask;
+#endif
        register int bit;
+#ifdef __LP64__
+       register uint32_t *maskp;
+#else
        register u_long *maskp;
+#endif
        register int sock;
        char cred_area[2*MAX_AUTH_BYTES + RQCRED_SIZE];
        msg.rm_call.cb_cred.oa_base = cred_area;
        msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
        r.rq_clntcred = &(cred_area[2*MAX_AUTH_BYTES]);
 
-
+#ifdef __LP64__
+       maskp = (uint32_t *)readfds->fds_bits;
+#else
        maskp = (u_long *)readfds->fds_bits;
+#endif
        for (sock = 0; sock <= svc_maxfd; sock += NFDBITS) {
            for (mask = *maskp++; (bit = ffs(mask)); mask ^= (1 << (bit - 1))) {
                if ((sock + bit) > (svc_maxfd + 1))
 
 
 int
 registerrpc(prognum, versnum, procnum, progname, inproc, outproc)
+       int prognum;
+       int versnum;
+       int procnum;
        char *(*progname)();
        xdrproc_t inproc, outproc;
 {
                        return (-1);
                }
        }
-       (void) pmap_unset((u_long)prognum, (u_long)versnum);
-       if (!svc_register(transp, (u_long)prognum, (u_long)versnum, 
+#ifdef __LP64__
+       (void) pmap_unset((uint32_t)prognum, (uint32_t)versnum);
+       if (!svc_register(transp, (uint32_t)prognum, (uint32_t)versnum, 
            universal, IPPROTO_UDP)) {
                (void) fprintf(stderr, "couldn't register prog %d vers %d\n",
                    prognum, versnum);
                return (-1);
        }
+#else
+       (void) pmap_unset((u_long)prognum, (u_long)versnum);
+       if (!svc_register(transp, (u_long)prognum, (u_long)versnum, 
+           universal, IPPROTO_UDP)) {
+                       (void) fprintf(stderr, "couldn't register prog %d vers %d\n",
+                       prognum, versnum);
+               return (-1);
+       }
+#endif
        pl = (struct proglst *)malloc(sizeof(struct proglst));
        if (pl == NULL) {
                (void) fprintf(stderr, "registerrpc: out of memory\n");
 
 
 struct tcp_conn {  /* kept in xprt->xp_p1 */
        enum xprt_stat strm_stat;
+#ifdef __LP64__
+       uint32_t x_id;
+#else
        u_long x_id;
+#endif
        XDR xdrs;
        char verf_body[MAX_AUTH_BYTES];
 };
 
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdint.h>
 #include <string.h>
 #include <unistd.h>
 #include <rpc/rpc.h>
  */
 struct svcudp_data {
        u_int   su_iosz;        /* byte size of send.recv buffer */
+#ifdef __LP64__
+       uint32_t        su_xid;         /* transaction id */
+#else
        u_long  su_xid;         /* transaction id */
+#endif
        XDR     su_xdrs;        /* XDR handle */
        char    su_verfbody[MAX_AUTH_BYTES];    /* verifier body */
        char *  su_cache;       /* cached data, NULL if no cache */
        register XDR *xdrs = &(su->su_xdrs);
        register int rlen;
        char *reply;
+#ifdef __LP64__
+       uint32_t replylen;
+#else
        u_long replylen;
+#endif
 
     again:
        xprt->xp_addrlen = sizeof(struct sockaddr_in);
        rlen = recvfrom(xprt->xp_sock, rpc_buffer(xprt), (int) su->su_iosz, 0, (struct sockaddr *)&(xprt->xp_raddr), (unsigned int *)&(xprt->xp_addrlen));
        if (rlen == -1 && errno == EINTR)
                goto again;
+#ifdef __LP64__
+       if (rlen < 4*sizeof(uint32_t))
+               return (FALSE);
+#else
        if (rlen < 4*sizeof(u_long))
                return (FALSE);
+#endif
        xdrs->x_op = XDR_DECODE;
        XDR_SETPOS(xdrs, 0);
        if (! xdr_callmsg(xdrs, msg))
                    == slen) {
                        stat = TRUE;
                        if (su->su_cache && slen >= 0) {
+#ifdef __LP64__
+                               cache_set(xprt, (uint32_t) slen);
+#else
                                cache_set(xprt, (u_long) slen);
+#endif
                        }
                }
        }
        /*
         * Index into cache is xid, proc, vers, prog and address
         */
+#ifdef __LP64__
+       uint32_t cache_xid;
+       uint32_t cache_proc;
+       uint32_t cache_vers;
+       uint32_t cache_prog;
+#else
        u_long cache_xid;
        u_long cache_proc;
        u_long cache_vers;
        u_long cache_prog;
+#endif
        struct sockaddr_in cache_addr;
        /*
         * The cached reply and length
         */
        char * cache_reply;
+#ifdef __LP64__
+       uint32_t cache_replylen;
+#else
        u_long cache_replylen;
+#endif
        /*
         * Next node on the list, if there is a collision
         */
  * The entire cache
  */
 struct udp_cache {
+#ifdef __LP64__
+       uint32_t uc_size;               /* size of cache */
+#else
        u_long uc_size;         /* size of cache */
+#endif
        cache_ptr *uc_entries;  /* hash table of entries in cache */
        cache_ptr *uc_fifo;     /* fifo list of entries in cache */
+#ifdef __LP64__
+       uint32_t uc_nextvictim; /* points to next victim in fifo list */
+       uint32_t uc_prog;               /* saved program number */
+       uint32_t uc_vers;               /* saved version number */
+       uint32_t uc_proc;               /* saved procedure number */
+#else
        u_long uc_nextvictim;   /* points to next victim in fifo list */
        u_long uc_prog;         /* saved program number */
        u_long uc_vers;         /* saved version number */
        u_long uc_proc;         /* saved procedure number */
+#endif 
        struct sockaddr_in uc_addr; /* saved caller's address */
 };
 
 int
 svcudp_enablecache(transp, size)
        SVCXPRT *transp;
+#ifdef __LP64__
+       uint32_t size;
+#else
        u_long size;
+#endif
 {
        struct svcudp_data *su = su_data(transp);
        struct udp_cache *uc;
 static void
 cache_set(xprt, replylen)
        SVCXPRT *xprt;
-       u_long replylen;        
+#ifdef __LP64__
+       uint32_t replylen;
+#else
+       u_long replylen;
+#endif
 {
        register cache_ptr victim;      
        register cache_ptr *vicp;
        SVCXPRT *xprt;
        struct rpc_msg *msg;
        char **replyp;
+#ifdef __LP64__
+       uint32_t *replylenp;
+#else
        u_long *replylenp;
+#endif
 {
        u_int loc;
        register cache_ptr ent;