Make connection cache aware of port and flags.
Add xasprintf() to the suggested alternatives, it can replace the malloc()+sprintf() idiom.