Praise be to Allah and peace be upon the Messenger of Allah. To the point:
I have read the article of Dr. Taqy  Al-Din Al-Hilaly (may Allah be pleased with him) which was published on  Al-Alukah site, titled "Manqabat Lil-Malik Faysal Qadas Allah Ruhahu" (The virtues of King Faysal (may Allah bestow mercy on his soul)).  The article was published in eleventh edition of the magazine of  Islamic Researches. It was about the stance of Dr. Maurice Bocay toward  Islam. I wanted to draw attention to something mentioned therein which  is whether is it possible to know the gender of the fetus inside the  womb, which was impossible to know at the time of Dr. Maurice Bocay.
 
Since cognitive development - particularly in the field of medicine – has taken place, we see new developments such asthe possibility of recognizing the gender of a baby through sonic-rays or others.
 
No doubt that would confuse some people because Allah says:"Verily Allâh, with Him (Alone) is the knowledge of the Hour, He sends down the rain, and knows that which is in the wombs."
 For that reason, I wanted to explain this Ayah.
Of the most precise answers that I have seen tackling this issue are:
1- The  Ayah and the similar ones are talking about the unseen and do not speak  about witnessing the process. So, whoever claims that he can know the  gender of the fetus inside his mother's womb, he is just foreseeing but  as for those who use devices to see the baby, he moved from unseen to  realities as for those who see the baby coming out from the mother's  belly. Let us explain this by parables:
 
If one of the infidels of Makkah were to defy the Prophet (peace be upon him) saying:I can know what is inside the womb then came to a pregnant woman and opened her belly, looked at the fetus and said:It is a boy. Will this be a challenge?
 
I have no  doubt if this happens, the first to be laugh at him is his friends  because the context of the Ayah is about the world of the Unseen and is  not about reality.
 
If we to  accept this, what is the difference between to knife a woman's belly or  to put a camera in her womb to photo the fetus inside the womb or rays  that penetrate the wall of the abdomen to the womb!
 
The case is  back again to realities not the Unseen world and this is enough to  remove ambiguity from this issue. Allah knows the best.
 2- The other explanation is:By thinking about the word "Ma" in Arabic which was mentioned in the Ayah:"what in the hearts",  It is a form of generality at the linguists, so the gender of the baby  is not the only thing meant by the Ayah but the meaning is knowing  everything about what is in there. For example,
1- whether a male or female?
2- beautiful or ugly?
3- how long shall he/she live?
4- rich or poor?
5- Muslim or non-Muslim?
6- miserable or happy?
7- short or tall? Black or white, lives in the east or the west, will  marry or not, will have children or not and how many are they? You can  numerate the descriptions that apply to all humans.
 
Does the knowledge of one of these issues indicate the meaning of the word "What"
 Al-Bukhari and Muslim reported in their Sahih from `Abdullah ibn Mas`ud (may Allah be pleased with him) said:Allah's Messenger (peace be upon him), the true and truly inspired said:"(The matter of the Creation of)  a human being is put together in the womb of the mother in forty days,  and then he becomes a clot of thick blood for a similar period, and then  a piece of flesh for a similar period. Then Allah sends an angel who is  ordered to write four things.He is ordered to write down his (i.e. the new creature's) deeds, his livelihood, his (date of)  death, and whether he will be blessed or wretched (in religion). Then  the soul is breathed into him. So, a man amongst you may do (good deeds  till there is only a cubit between him and Paradise and then what has  been written for him decides his behavior and he starts doing (evil) deeds characteristic of the people of the (Hell) Fire. And similarly a man amongst you may do (evil)  deeds till there is only a cubit between him and the (Hell) Fire, and  then what has been written for him decides his behavior, and he starts  doing deeds characteristic of the people of Paradise."
 This  Hadith indicates that the conditions of the fetus inside the womb is  not confined to be male or female. May Allah grant us success for He is  the guider to the straight way!