+ } else {
+ logger(DEBUG_CONNECTIONS, LOG_ERR, "Could not send %d bytes of data to %s (%s): %s", c->outbuf.len - c->outbuf.offset, c->name, c->hostname, sockstrerror(sockerrno));
+ }
+
+ terminate_connection(c, c->edge);
+ return;
+ }
+
+ buffer_read(&c->outbuf, outlen);
+ if(!c->outbuf.len)
+ io_set(&c->io, IO_READ);
+}
+
+static void handle_meta_io(void *data, int flags) {
+ connection_t *c = data;
+
+ if(c->status.connecting) {
+ /*
+ The event loop does not protect against spurious events. Verify that we are actually connected
+ by issuing an empty send() call.
+
+ Note that the behavior of send() on potentially unconnected sockets differ between platforms:
+ +------------+-----------+-------------+-----------+
+ | Event | POSIX | Linux | Windows |
+ +------------+-----------+-------------+-----------+
+ | Spurious | ENOTCONN | EWOULDBLOCK | ENOTCONN |
+ | Failed | ENOTCONN | (cause) | ENOTCONN |
+ | Successful | (success) | (success) | (success) |
+ +------------+-----------+-------------+-----------+
+ */
+ if (send(c->socket, NULL, 0, 0) != 0) {
+ if (sockwouldblock(sockerrno))
+ return;
+ int socket_error;
+ if (!socknotconn(sockerrno))
+ socket_error = sockerrno;
+ else {
+ socklen_t len = sizeof socket_error;
+ getsockopt(c->socket, SOL_SOCKET, SO_ERROR, (void *)&socket_error, &len);
+ }
+ if (socket_error) {
+ logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Error while connecting to %s (%s): %s", c->name, c->hostname, sockstrerror(socket_error));
+ terminate_connection(c, false);
+ }
+ return;
+ }
+
+ c->status.connecting = false;
+ finish_connecting(c);
+ }
+
+ if(flags & IO_WRITE)
+ handle_meta_write(c);
+ else
+ handle_meta_connection_data(c);
+}
+
+bool do_outgoing_connection(outgoing_t *outgoing) {
+ char *address, *port, *space;
+ struct addrinfo *proxyai = NULL;
+ int result;
+
+begin:
+ if(!outgoing->ai) {
+ if(!outgoing->cfg) {
+ logger(DEBUG_CONNECTIONS, LOG_ERR, "Could not set up a meta connection to %s", outgoing->name);
+ retry_outgoing(outgoing);
+ return false;