Try something like this (untested):
#! /bin/sh
fifo=/tmp/discover-vnc.fifo
rm -f $fifo
mkfifo $fifo
dns-sd -B _rfb._tcp >$fifo &
pid=$!
while read line; do
case $line in *_rfb._tcp.*)
# do something here.. then test
# if flag=3 and break otherwise
esac
done <$fifo
kill $pid