--- a/daemons/lircd.cpp
+++ b/daemons/lircd.cpp
@@ -901,43 +901,41 @@
 {
 	int r;
 	char service[64];
-	struct addrinfo* addr;
-	struct addrinfo* tmp;
+	struct addrinfo* addrinfos;
+	struct addrinfo* a;
 	int enable = 1;
 
 	snprintf(service, sizeof(service), "%d", peer->port);
 	peer->socket = socket(AF_INET, SOCK_STREAM, 0);
-	r = getaddrinfo(peer->host, service, NULL, &addr);
+	r = getaddrinfo(peer->host, service, NULL, &addrinfos);
 	if (r != 0) {
-		log_error("Name lookup failure connecting to %s",
-			  peer->host);
-		peer->connection_failure++;
-		gettimeofday(&peer->reconnect, NULL);
-		peer->reconnect.tv_sec += 5 * peer->connection_failure;
-		close(peer->socket);
-		peer->socket = -1;
-		return;
+		log_perror_err("Name lookup failure connecting to %s",
+			       peer->host);
+		goto errexit;
 	}
 	(void)setsockopt(peer->socket,
 			 SOL_SOCKET, SO_KEEPALIVE, &enable, sizeof(enable));
-        do {
-		r = connect(peer->socket,
-			    addr->ai_addr, sizeof(struct sockaddr));
-		tmp = addr;
-	addr = addr->ai_next;
-		freeaddrinfo(tmp);
-	} while (r < 0 && addr != NULL);
+	for (a = addrinfos; a != NULL; a = a->ai_next) {
+		r = connect(peer->socket, a->ai_addr, a->ai_addrlen);
+		if (r >= 0)
+			break;
+	}
+	freeaddrinfo(addrinfos);
 	if (r == -1) {
 		log_perror_err("Cannot connect to %s", peer->host);
-		peer->connection_failure++;
-		gettimeofday(&peer->reconnect, NULL);
-		peer->reconnect.tv_sec += 5 * peer->connection_failure;
-		close(peer->socket);
-		peer->socket = -1;
-		return;
-	}
-	log_notice("connected to %s", peer->host);
+		goto errexit;
+	}
+	log_notice("Connected to %s", peer->host);
 	peer->connection_failure = 0;
+	return;
+
+errexit:
+	peer->connection_failure++;
+	gettimeofday(&peer->reconnect, NULL);
+	peer->reconnect.tv_sec += 5 * peer->connection_failure;
+	close(peer->socket);
+	peer->socket = -1;
+	return;
 }


--- a/lib/lirc_client.c
+++ b/lib/lirc_client.c
@@ -2050,28 +2050,25 @@
 
 int lirc_get_remote_socket(const char* address, int port, int quiet)
 {
-	struct addrinfo* host;
-	struct addrinfo* tmp;
+	struct addrinfo* addrinfos;
+	struct addrinfo* a;
 	char service[64];
 	int r;
 
 	snprintf(service, sizeof(service),
 		 "%d", port > 0 ? port : LIRC_INET_PORT);
-	r = getaddrinfo(address, service, NULL, &host);
+	r = getaddrinfo(address, service, NULL, &addrinfos);
 	if (r < 0) {
 		if (!quiet)
 			fprintf(stderr, "get_remote_socket: host %s unknown\n",
 				address);
 		return -EADDRNOTAVAIL;
 	}
-	do {
-		r = do_connect(host->ai_family,
-			       host->ai_addr,
-			       sizeof(host->ai_addr),
-			       quiet);
-		tmp = host;
-		host = host->ai_next;
-		freeaddrinfo(tmp);
-	} while (r < 0 && host != NULL);
+	for (a = addrinfos; a != NULL; a = a->ai_next) {
+		r = do_connect(a->ai_family, a->ai_addr, a->ai_addrlen, quiet);
+		if (r >= 0)
+			break;
+	};
+	freeaddrinfo(addrinfos);
 	return r;
 }
