A way to achieve your goal is to use the 'catch' command.
This command catches the error and gives you a return-code and a string indicating the result of the command:
set rc [catch {socket $host $port} msg}
if {$rc == 0} {
# all is ok and msg holds the result of the command
set channel $msg
} else {
# some error occured and msg holds the error message
log "can't open port $port on $host\nreason: $msg"
}
Excerpt of the Tcl manual:
The catch command may be used to prevent errors from aborting command interpretation. Catch calls the Tcl interpreter recursively to execute script, and always returns without raising an error, regardless of any errors that might occur while executing script.
If script raises an error, catch will return a non-zero integer value corresponding to one of the exceptional return codes (see tcl.h for the definitions of code values). If the varName argument is given, then the variable it names is set to the error message from interpreting script.
If script does not raise an error, catch will return 0 (TCL_OK) and set the variable to the value returned from script.
HTH
ulis