Validate all function arguments and check for an opened connection

Fixes #1 and #5

This should check the input types of all the function arguments, it also
insists on an open connection being used for everything except open()
(which requires a closed connection).

I also fixed another memory leak.