Index: filed.c
==================================================================
--- filed.c
+++ filed.c
@@ -935,21 +935,21 @@
 		log->http_code = -1;
 		log->reason = "fdopen_failed";
 
 		filed_log_entry(log);
 
-		return(0);
+		return(FILED_CONNECTION_CLOSE);
 	}
 
 	request = filed_get_http_request(fp, request, options);
 
 	if (request == NULL) {
 		log->buffer[0] = '\0';
 
 		filed_error_page(fp, date_current, 500, FILED_REQUEST_METHOD_GET, "format", log);
 
-		return(0);
+		return(FILED_CONNECTION_CLOSE);
 	}
 
 	path = request->path;
 	strcpy(log->buffer, path);
 	log->method = request->method;
@@ -956,28 +956,28 @@
 
 	/* If the requested path is a directory, redirect to index page */
 	if (request->type == FILED_REQUEST_TYPE_DIRECTORY) {
 		filed_redirect_index(fp, date_current, path, log);
 
-		return(0);
+		return(FILED_CONNECTION_CLOSE);
 	}
 
 	fileinfo = filed_open_file(path, &request->fileinfo);
 	if (fileinfo == NULL) {
 		filed_error_page(fp, date_current, 404, request->method, "open_failed", log);
 
-		return(0);
+		return(FILED_CONNECTION_CLOSE);
 	}
 
 	if (request->headers.range.present) {
 		if (request->headers.range.offset != 0 || request->headers.range.length >= 0) {
 			if (request->headers.range.offset >= fileinfo->len) {
 				filed_error_page(fp, date_current, 416, request->method, "range_invalid", log);
 
 				close(fileinfo->fd);
 
-				return(0);
+				return(FILED_CONNECTION_CLOSE);
 			}
 
 			if (request->headers.range.length == ((off_t) -1)) {
 				filed_log_msg_debug("Computing length to fit in bounds: fileinfo->len = %llu, request->headers.range.offset = %llu",
 					(unsigned long long) fileinfo->len,
@@ -1097,20 +1097,20 @@
 
 	log->endtime = (time_t) -1;
 	log->sent_length = sendfile_sent;
 
 	filed_log_entry(log);
+
+	close(fileinfo->fd);
 
 	if (request->headers.connection != FILED_CONNECTION_KEEP_ALIVE) {
-		close(fileinfo->fd);
-
 		fclose(fp);
 
-		return(0);
+		return(FILED_CONNECTION_CLOSE);
 	}
 
-	return(1);
+	return(FILED_CONNECTION_KEEP_ALIVE);
 }
 
 /* Handle incoming connections */
 static void *filed_worker_thread(void *arg_v) {
 	struct filed_worker_thread_args *arg;
@@ -1118,11 +1118,11 @@
 	struct filed_log_entry *log, local_dummy_log;
 	struct filed_options *options;
 	struct sockaddr_in6 addr;
 	socklen_t addrlen;
 	int failure_count = 0, max_failure_count = FILED_MAX_FAILURE_COUNT;
-	int accept_new = 1;
+	int connection_state = FILED_CONNECTION_CLOSE;
 	int master_fd, fd = -1;
 
 	/* Read arguments */
 	arg = arg_v;
 
@@ -1143,13 +1143,15 @@
 			break;
 		}
 
 		log->type = FILED_LOG_TYPE_TRANSFER;
 
-		/* Accept a new client */
-		if (accept_new) {
+		/* If we closed the old connection, accept a new one */
+		if (connection_state == FILED_CONNECTION_CLOSE) {
+			/* Accept a new client */
 			addrlen = sizeof(addr);
+
 			fd = accept(master_fd, (struct sockaddr *) &addr, &addrlen);
 		}
 
 		/*
 		 * If we fail, make a note of it so we don't go into a loop of
@@ -1176,11 +1178,11 @@
 
 		/* Reset failure count*/
 		failure_count = 0;
 
 		/* Handle socket */
-		accept_new = !filed_handle_client(fd, &request, log, options);
+		connection_state = filed_handle_client(fd, &request, log, options);
 	}
 
 	/* Report error */
 	filed_log_msg("THREAD_DIED ABNORMAL");