Changes as suggested by review:
 set err for connection to EINVAL
 cleanups