I am trying to use EGL with X11, but I am having some trouble.

That's the EGL related code. Error checking code has been stripped for brevity:

Code :
EGLBoolean CreateEGLContext ( EGLNativeWindowType hWnd, EGLDisplay* eglDisplay,
                              EGLContext* eglContext, EGLSurface* eglSurface,
                              EGLint attribList[])
{
    ...
 
    // Get Display
    display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
 
    // Initialize EGL
    eglInitialize(display, &majorVersion, &minorVersion)
 
    // Get configs
    eglGetConfigs(display, NULL, 0, &numConfigs)
 
    // Choose config
    eglChooseConfig(display, attribList, &config, 1, &numConfigs)
 
    // Create a surface
    surface = eglCreateWindowSurface(display, config, (EGLNativeWindowType)hWnd, NULL);
 
    // Create a GL context
    context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs );
 
    // Make the context current
    eglMakeCurrent(display, surface, surface, context)
 
    ...
}
And it chokes on eglMakeCurrent, throwing a segmentation fault signal and outputting these error messages:

X Error: code 146 major 135 minor 5: GLXBadDrawable.
X Error: code 3 major 3 minor 0: BadWindow (invalid Window parameter).
X Error: code 3 major 3 minor 0: BadWindow (invalid Window parameter).
Segmentation fault
Of course one would wonder where's the connection with X11 here? It's in the hWnd, which is obtained from this code:

Code :
scr = DefaultScreen(dpy);
rootwin = RootWindow(dpy, scr);
cmap = DefaultColormap(dpy, scr);
 
win=XCreateSimpleWindow(dpy, rootwin,
        0, 0, esContext->width, esContext->height, 0,
        BlackPixel(dpy, scr), BlackPixel(dpy, scr));
 
XStoreName(dpy, win, "opengles2.0");
 
gc=XCreateGC(dpy, win, 0, NULL);
XSetForeground(dpy, gc, WhitePixel(dpy, scr));
 
XSelectInput(dpy, win, ExposureMask|ButtonPressMask);
 
XMapWindow(dpy, win);
 
/*
 * Link the windows handle to EGL
 */
esContext->hWnd = win;
Now note that if I set hWnd to NULL it does not show an error but just doesn't do anything (i.e. doesn't open a window).

Any suggestions would be highly appreciated! Thanks!