diff --git a/lib/modules/Protocols.pmod/DNS.pmod b/lib/modules/Protocols.pmod/DNS.pmod
index 36d7e70e310317f157a3743edeb5a9604adcbe77..9295aa922ac909741bcf08ca0d3e7261f65d1f71 100644
--- a/lib/modules/Protocols.pmod/DNS.pmod
+++ b/lib/modules/Protocols.pmod/DNS.pmod
@@ -563,13 +563,16 @@ class client
       udp->send(nameservers[i % sizeof(nameservers)], 53, s);
 
       while (udp->wait(RETRY_DELAY)) {
-	m = udp->read();
-	if ((m->port == 53) &&
-	    (m->data[0..1] == s[0..1]) &&
-	    (search(nameservers, m->ip) != -1)) {
-	  // Success.
-	  return decode_res(m->data);
-	}
+	// udp->read() can throw an error on connection refused.
+	catch {
+	  m = udp->read();
+	  if ((m->port == 53) &&
+	      (m->data[0..1] == s[0..1]) &&
+	      (search(nameservers, m->ip) != -1)) {
+	    // Success.
+	    return decode_res(m->data);
+	  }
+	};
       }
     }
     // Failure.