Patch to check for fcntl success

Static analysis (coverity) detected that the
system call fcntl did not check its return
value.  This patch adds in that check.

This patch also implements a more robust setting
of the fd flags (get the current flags, or'ing in
the NONBLOCK value, and then setting the flags).

Fixes coverity 1028691
Fixes #11610
strip trailing whitespace