pick coorelation opcode error

this is my first time trying pick coorelation, when following the directions my book gives me, i get this error…

X Error of failed request: GLXBadContextState
Major opcode of failed request: 143 (GLX)
Minor opcode of failed request: 5 (X_GLXMakeCurrent)
Serial number of failed request: 133
Current serial number in output stream: 133

what im tyring to do is if there is something under the mouse when it is clicked, then i call a function, if there is nothing, then do a popup menu. i know that the reason why im getting this error is because im trying to call the display function (the registered glutDisplayFunc) before the display mode is at GL_RENDER. but if i put the display() after changing modes, the popup menu will not show up. here is my code, can anyone help… thanks

void mouse(int button, int state, int x, int y){
  GLuint selectBuf[512];
  GLint hits;
  GLint viewport[4];

  if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
    glGetIntegerv(GL_VIEWPORT, viewport);

    glSelectBuffer(512, selectBuf);
    glRenderMode(GL_SELECT);

    glInitNames();
    glPushName(0);

    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    gluPickMatrix((GLdouble)x, (GLdouble)(viewport[3]-y), 5.0, 5.0, viewport);

    //set back to original ORTHO view
    glOrtho(0.0,300.0,0.0,300.0,0.0,300.0);
    //call registered display function
    display();

    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glFlush();

    //#number of objects under click
    hits=glRenderMode(GL_RENDER);

    if(hits>0){
      //process hits
      //#of hits = hits
      //vector of hits = selectBuf
      cout << "HITS:" << hits << endl;
    }
    else{
      cout << "NOHITS" << endl;
      //no hits, display menu
      glutCreateMenu(selectObject);
      glutAddMenuEntry("Sphere",1);
      glutAddMenuEntry("Cube",2);
      glutAddMenuEntry("Torus",3);
      glutAddMenuEntry("Icosahedron",4);
      glutAddMenuEntry("Octahedron",5);
      glutAddMenuEntry("Tetrahedron",6);
      glutAddMenuEntry("Dodecahedron",7);
      glutAddMenuEntry("Cone",8);
      glutAddMenuEntry("Teapot",9);
      glutAttachMenu(GLUT_LEFT_BUTTON);
      glutPostRedisplay();
    }
  }
}

What book is that?

Have you looked at the selection and menu demos? They work.