diff -Naur motion-20060711-051001_orig/netcam.c motion-20060711-051001/netcam.c
--- motion-20060711-051001_orig/netcam.c	2006-07-15 11:52:00.000000000 +0200
+++ motion-20060711-051001/netcam.c	2006-07-17 12:33:04.000000000 +0200
@@ -594,7 +594,6 @@
 			motion_log(LOG_ERR, 1, "netcam_disconnect");
 
 		netcam->sock = -1;
-		rbuf_discard(netcam);
 	}
 }
 
@@ -660,16 +659,14 @@
 
 	if ((saveflags = fcntl(netcam->sock, F_GETFL, 0)) < 0) {
 		motion_log(LOG_ERR, 1, "fcntl(1) on socket");
-		close(netcam->sock);
-		netcam->sock = -1;
+		netcam_disconnect(netcam);
 		return -1;
 	}
 
 	/* Set the socket non-blocking */
 	if (fcntl(netcam->sock, F_SETFL, saveflags | O_NONBLOCK) < 0) {
 		motion_log(LOG_ERR, 1, "fcntl(2) on socket");
-		close(netcam->sock);
-		netcam->sock = -1;
+		netcam_disconnect(netcam);
 		return -1;
 	}
 
@@ -682,8 +679,7 @@
 	if ((ret < 0) && (back_err != EINPROGRESS)) {
 		if (!err_flag)
 			motion_log(LOG_ERR, 1, "connect() failed (%d)", back_err);
-		close(netcam->sock);
-		netcam->sock = -1;
+		netcam_disconnect(netcam);
 		return -1;
 	}
 
@@ -697,8 +693,7 @@
 	if (ret == 0) {            /* 0 means timeout */
 		if (!err_flag)
 			motion_log(LOG_ERR, 0, "timeout on connect()");
-		close(netcam->sock);
-		netcam->sock = -1;
+		netcam_disconnect(netcam);
 		return -1;
 	}
 
@@ -1682,12 +1677,14 @@
 		free(netcam->jpegbuf);
 	}
 
-	if (netcam->response != NULL) {
-		free(netcam->response);
-	}
-
 	if (netcam->ftp != NULL) {
 		ftp_free_context(netcam->ftp);
+	} else {
+		netcam_disconnect(netcam);
+	}
+
+	if (netcam->response != NULL) {
+		free(netcam->response);
 	}
 
 	pthread_mutex_destroy(&netcam->mutex);
diff -Naur motion-20060711-051001_orig/netcam_wget.c motion-20060711-051001/netcam_wget.c
--- motion-20060711-051001_orig/netcam_wget.c	2006-07-11 05:10:02.000000000 +0200
+++ motion-20060711-051001/netcam_wget.c	2006-07-17 12:32:04.000000000 +0200
@@ -282,8 +282,7 @@
 {
 	if (!netcam->response->buffer_left) {
 		int res;
-		netcam->response->buffer_pos = netcam->response->buffer;
-		netcam->response->buffer_left = 0;
+		rbuf_initialize(netcam);
 		res = netcam_recv (netcam, netcam->response->buffer,
 		                   sizeof (netcam->response->buffer));
 
@@ -318,13 +317,6 @@
 	}
 }
 
-/* Discard any cached data in RBUF.  */
-void rbuf_discard (netcam_context_ptr netcam)
-{
-	netcam->response->buffer_left = 0;
-	netcam->response->buffer_pos = netcam->response->buffer;
-}
-
 /* Get the HTTP result code */
 int http_result_code(const char *header)
 {
diff -Naur motion-20060711-051001_orig/netcam_wget.h motion-20060711-051001/netcam_wget.h
--- motion-20060711-051001_orig/netcam_wget.h	2006-07-11 05:10:02.000000000 +0200
+++ motion-20060711-051001/netcam_wget.h	2006-07-17 12:33:42.000000000 +0200
@@ -65,7 +65,6 @@
 int rbuf_readchar(netcam_context_ptr, char *);
 int rbuf_peek(netcam_context_ptr, char *);
 int rbuf_flush(netcam_context_ptr, char *, int);
-void rbuf_discard(netcam_context_ptr);
 
 /* Internal, but used by the macro. */
 int rbuf_read_bufferful(netcam_context_ptr);
 