Index: /neercs/trunk/src/attach.c
===================================================================
--- /neercs/trunk/src/attach.c	(revision 2487)
+++ /neercs/trunk/src/attach.c	(revision 2488)
@@ -14,5 +14,5 @@
 #include "neercs.h"
 
-static char * build_socket_path(char *socket_dir)
+static char * build_socket_path(char *socket_dir, char *session_name)
 {
     char *path, *dir;
@@ -26,5 +26,5 @@
         dir = "/tmp";
     if(path)
-        snprintf(path, PATH_MAX+1, "%s/neercs.%d.sock", dir, getpid());
+        snprintf(path, PATH_MAX+1, "%s/neercs.%s.sock", dir, session_name);
     return path;
 }
@@ -46,5 +46,5 @@
 
     myaddr.sun_family = AF_UNIX;
-    screen_list->socket_path = build_socket_path(screen_list->socket_path);
+    screen_list->socket_path = build_socket_path(screen_list->socket_path, screen_list->session_name);
     strncpy(myaddr.sun_path, screen_list->socket_path, sizeof(myaddr.sun_path) - 1);
     unlink(myaddr.sun_path);
@@ -159,5 +159,5 @@
 }
 
-char ** list_sockets(char *socket_dir)
+char ** list_sockets(char *socket_dir, char *session_name)
 {
     char *pattern, *dir;
@@ -179,5 +179,8 @@
         return globbuf.gl_pathv;
 
-    snprintf(pattern, PATH_MAX, "%s/neercs.*.sock", dir);
+    if(session_name && strlen(session_name)+strlen(dir)+13<PATH_MAX)
+        sprintf(pattern, "%s/neercs.%s.sock", dir, session_name);
+    else
+        snprintf(pattern, PATH_MAX, "%s/neercs.*.sock", dir);
     pattern[PATH_MAX] = '\0';
 
Index: /neercs/trunk/src/main.c
===================================================================
--- /neercs/trunk/src/main.c	(revision 2487)
+++ /neercs/trunk/src/main.c	(revision 2488)
@@ -68,7 +68,9 @@
     printf("Example : %s zsh top \n\n", argv[0]);
     printf("Options :\n");
-    printf("\t--config\t\t-c <file>\t\tuse given config file\n");
-    printf("\t--pid\t\t-P <pid>\t\tattach <pid>\n");
-    printf("\t\t\t-R\t\t\tre-attach another neercs\n");
+    printf("\t--config\t-c <file>\t\tuse given config file\n");
+    printf("\t--pid\t\t-P <pid>\t\tgrab <pid>\n");
+    printf("\t\t\t-r [session]\t\treattach to a detached neercs\n");
+    printf("\t\t\t-R [session]\t\treattach if possible, otherwise start a new session\n");
+    printf("\t\t\t-S <name>\t\tname this session <name> instead of <pid>\n");
     printf("\t--version\t-v \t\t\tdisplay version and exit\n");
     printf("\t--help\t\t-h \t\t\tthis help\n");
@@ -82,5 +84,5 @@
     struct recurrent_list *recurrent_list = NULL;
     struct passwd *user_info;
-    char *default_shell = NULL, *user_path = NULL;
+    char *default_shell = NULL, *user_path = NULL, *session_name = NULL;
     int i, w, h, args, s=0;
     int eof = 0, refresh = 1, command = 0;
@@ -88,5 +90,5 @@
     int lock_offset = 0;
     int mainret = 0;
-    int attach = 0;
+    int attach = 0, forceattach = 0;
     int *to_grab = NULL;
     int nb_to_grab = 0;
@@ -142,4 +144,5 @@
     screen_list->socket_dir = NULL;
     screen_list->socket_path = NULL;
+    screen_list->session_name = NULL;
     memset(screen_list->lockmsg, 0, 1024);
     memset(screen_list->lockpass, 0, 1024);
@@ -168,7 +171,7 @@
             };
 #if defined USE_GRAB
-        int c = mygetopt(argc, argv, "c:RP:hv", long_options, &option_index);
+        int c = mygetopt(argc, argv, "c:S:R::r::P:hv", long_options, &option_index);
 #else
-        int c = mygetopt(argc, argv, "c:Rhv", long_options, &option_index);
+        int c = mygetopt(argc, argv, "c:S:R::r::hv", long_options, &option_index);
 #endif
         if(c == -1)
@@ -181,4 +184,10 @@
                 free(user_path);
             user_path = strdup(myoptarg);
+            s+=2;
+            break;
+        case 'S':
+            if(screen_list->session_name)
+                free(screen_list->session_name);
+            screen_list->session_name = strdup(myoptarg);
             s+=2;
             break;
@@ -205,5 +214,17 @@
             s+=2;
             break;
+        case 'r':
+            forceattach = 1;
         case 'R':
+            if(attach)
+            {
+                fprintf(stderr, "Attaching can only be requested once\n");
+                return -1;
+            }
+            if(myoptarg)
+            {
+                session_name = strdup(myoptarg);
+                s+=1;
+            }
             attach = 1;
             s+=1;
@@ -242,5 +263,9 @@
             return -1;
         }
-        sockets = list_sockets(screen_list->socket_dir);
+        sockets = list_sockets(screen_list->socket_dir, session_name);
+        if(!screen_list->session_name)
+            screen_list->session_name = session_name;
+        else
+            free(session_name);
         if(sockets && sockets[0])
         {
@@ -255,5 +280,20 @@
             fprintf(stderr, "No socket found!\n");
         }
-        return -1;
+        if(forceattach)
+            return -1;
+    }
+
+    /* Build default session name */
+    if(!screen_list->session_name)
+    {
+        char mypid[32]; /* FIXME Compute the length of PID_MAX ? */
+        snprintf(mypid, 31, "%d", getpid());
+        mypid[31]= '\0';
+        screen_list->session_name = strdup(mypid);
+        if(!screen_list->session_name)
+        {
+            fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__);
+            return -1;
+        }
     }
 
Index: /neercs/trunk/src/neercs.h
===================================================================
--- /neercs/trunk/src/neercs.h	(revision 2487)
+++ /neercs/trunk/src/neercs.h	(revision 2488)
@@ -76,4 +76,5 @@
     char *socket_path;           /* Socket to ask for attaching */
     char *socket_dir;            /* Where to create the socket */
+    char *session_name;          /* Name of the session */
 
     /* Lock */
@@ -138,5 +139,5 @@
 int create_socket(struct screen_list* screen_list);
 int read_socket(struct screen_list* screen_list, cucul_canvas_t * cv, caca_display_t ** dp);
-char ** list_sockets(char *socket_dir);
+char ** list_sockets(char *socket_dir, char *session_name);
 
 /* Screens management */
