User should be able to type a sequence of messages, and server program will echo messages back to user. However, the program begins an infinite loop of printing that message. How do I prevent this?
int max_fd = sock_fd;
if (STDIN_FILENO > sock_fd) {
    max_fd = STDIN_FILENO;
}
fd_set all_fds;
FD_ZERO(&all_fds);
FD_SET(sock_fd, &all_fds);
FD_SET(STDIN_FILENO, &all_fds);

// Read input from the user and send it to the server. Echo any output
// received from the server.
while (1) {
    fd_set listen_fds = all_fds;
    if (select(max_fd + 1, &listen_fds, NULL, NULL, NULL) == -1) {
        perror("client: select");
        exit(1);
    }

    /*
        * We should really send "\r\n" too, so the server can identify partial
        * reads, but you are not required to handle partial reads in this lab.
        */
    if (write(sock_fd, buf, num_read) != num_read) {
        perror("client: write");
        close(sock_fd);
        exit(1);
    }

    // Check from stdin
    if (FD_ISSET(STDIN_FILENO, &listen_fds)) {
        num_read = read(STDIN_FILENO, buf, BUF_SIZE);
        if (num_read == 0) {
            break;
        }
        buf[num_read] = '\0';

        // Send user message to server
        if (write(sock_fd, buf, num_read) != num_read) {
            perror("client: write");
            close(sock_fd);
            exit(1);
        }
    }

    // Check from socket
    if (FD_ISSET(sock_fd, &listen_fds)) {
        num_read = read(sock_fd, buf, sizeof(buf) - 1);
        if (num_read == 0) {
            break;
        }
        buf[num_read] = '\0';

        // Print server message to stdout
        printf("[Server] %s", buf);
    }
}