$chan is not valid a socket event, you need to associate $chan with $sockname and store it either in a global variable, hash table, or by using the /sockmark command.