I think it could be done, well enough for sci-fi anyway. It would require a lot of high tech stuff. I don't think there would be enough room in the eye socket for a decent firearm. Something underpowered, sure. How vital are the parts of the brain directly behind the eye socket? If they are expendable, then you could have a tube running all the way back to the back of the skull and secured there, at the back of the eye socket, and at the front of the socket/skull with titanium.
Then you could put a recoil spring behind your gun to spread out the impact (make it occur over a longer period of time) and transfer it into the three plates.
The extra room would also give you space for a chamber of about a half inch, a decent sized rifled barrel of about two inches, plus a silencer/recoil compensator for the last inch.
You could have special cartridges made with flashless, quick burning powder to eliminate muzzle flash and reduce powder burn. Plus they could be loaded with just the right amount of power for that sized barrel.
Naturally, this would be a one shot deal. You could probably project a 95 grain 9mm bullet at about 800 feet per second, generating 135 foot pounds of energy. A little more than 1/3 the normal power of such a bullet, but still deadly enough with good shot placement.