Requires newer curl with --unix-socket from Debian version 9 codename Stretch.
Replace
$CURL \ --fail \ $CURL_VERBOSE \ --max-time 10 \ --output "$TEMP_DIR/socks_port_open_file" \ "$GATEWAY_IP:$SOCKS_PORT_WHONIXCHECK" \ &
with
$CURL \ --fail \ $CURL_VERBOSE \ --max-time 10 \ --output "$TEMP_DIR/socks_port_open_file" \ --unix-socket "/var/run/tor/socks" \ "file//var/run/tor/socks" \ &