This is not very idiomatic, you should just check if there is an error and return the connection object.
TCP and UDP would implement the same interface, no need to check for the type here.