+static int fdopen_read_write(int socket, FILE **read_fd, FILE **write_fd) {
+ *read_fd = fdopen(socket, "r");
+ if (*read_fd == NULL) {
+ perror("fdopen_read_write(): fdopen(\"r\") failed");
+ return -1;
+ }
+
+ *write_fd = fdopen(dup(socket), "w");
+ if (*write_fd == NULL) {
+ perror("fdopen_read_write(): fdopen(\"w\") failed");
+ fclose(*read_fd);
+ *read_fd = NULL; /* "tell" caller read_fd is already closed */
+ return -1;
+ }
+
+ return 0;
+}
+