Avoid fatal error when closing unopened port

Should fix #21. Also adds some tests around opening ports.