* call-seq:
 *      connect(target, [private_name], [all_messages]) -> nil
 * Connect to Spread. +target+ specifies which Spread daemon to
 * connect to. It must be a string in the form "port",
 * "port@hostname", or "port@ip".
 * +private_name+ is the name of this connection. It must be unique
 * among all the connections to a given Spread daemon. If not
 * specified, Spread will assign a randomly-generated unique private
 * name.
 * +all_messages+ indicates whether this connection should receive all
 * Spread messages, or just data messages. The default is +true+ (so
 * all messages will be received).
 * If this Spread::Connection instance is presently connected, it will
 * first be silently disconnected.
static VALUE
spconn_connect(int argc, VALUE *argv, VALUE obj)
    VALUE host_str;
    VALUE name_str;
    VALUE all_msgs;
    char *c_name_str;
    struct SpreadConnection *sp_conn;
    int n;

    Data_Get_Struct(obj, struct SpreadConnection, sp_conn);

    rb_scan_args(argc, argv, "12", &host_str, &name_str, &all_msgs);

    if (NIL_P(name_str))
        c_name_str = NULL;
        c_name_str = StringValuePtr(name_str);
    if (NIL_P(all_msgs))
        all_msgs = Qtrue; /* receive all messages by default */

    /* If we're currently connected, silently disconnect first */
    if (sp_conn->connected)


    if ((n = SP_connect(RSTRING(host_str)->ptr,
                        0, /* ignored */
                        sp_conn->private_group)) < 0)

    snprintf(sp_conn->spread_name, MAX_PROC_NAME, "%s",
    sp_conn->connected = true;

    return Qnil;