I assume with the two rak'a optional (sunnah) you mean the so called greeting of the mosque, as the sunnah prayers related to any mandatory (fard) prayer have their known timing! If you mean other optional prayers than you should know that it is more recommended to offer them at home.
Short answer: Once you enter a mosque you should offer a two rak'a optional prayer before sitting down.
Now to the long answer:
What to do in general if entering a mosque
There are details with some prayers (because of the fact that there are times one should avoid praying optional/nafl/sunnah prayers see my answer here):
In general if one enters a mosque one should offer a two rak'a prayer called tahiyyat al-masjid تحية المسجد or greeting of the mosque before sitting down according to the well known hadith (here the version from sahih al-Bukahri). Note that if the congregation starts and you couldn't complete the tahiyyat there are also differences in madhabs: Soms say if you can't reach the taslim before the Imam moves to ruku' you should do taslim and leave your optional prayer, else you should complete it and join the congregation. Some seem to say complete your prayer anyway.
Some madhabs (mainly shafi'i and hanbali) say one can do this anytime one enters a mosque (as to them this is considered as a reason for an exception as defined in my linked post). For example if you were crossing a mosque many times for other purposes you should at least doing it once (even if you were not waiting for a prayer). If you entered a mosque waiting for a prayer you should do it once you entered (as said with some details) before sitting down.
As most of the times where there are different views on whether it is allowed to pray the two rak'a tahiyyat al-masjid are related to times when offering prayers is at least frowned upon or even prohibited, in this fatwa in Arabic they say one can avoid the differences of madhabs if one waits until the time of "prohibition" ends and offer the tahiyyat.
Now to the exceptions with their details (AFAIK):
Sobh (fajr fard) prayer
A vast majority of scholars say: If you entered a mosque before the call for prayer (adhan) of sobh (fajr fard) you should do it, but if the adhan started you are asked to pray the fajr sunnah and this would be enough for you. However this fatwa in Arabic seems to says if you had time between the adhan and the iqamah you should perform the two rak'a tahiyyat al-masjid. While this fatwa (also in Arabic) says that one shouldn't pray after adhan more than the two rak'a sunnah and the two rak'a fard (based on this hadith). The maliki school has some different views: If somebody prayed the fajr sunnah at home and afterwards went to a mosque and had time to pray tahiyyat al-Masjid he either wait for sobh (because one shouldn't pray more than 4 rak'a until sunrise as this was the sunnah of the Prophet as said before) or (second view) is to pray the tahiyyat (based on the general meaning of do good in 22:77) (based on this fatwa in Arabic). This last view of the maliki school is also what the fatwa of (islamqa) affirms in case that one has offered fajr sunnah at home.
The maliki and hanafi school say one shouldn't pray two rak'a before al-maghrib. Maliki even said it is makruh (frowned upon). One of their sources is this hadith and the statement of the tabi'i Ibrahim an-Naakha'i إبراهيم بن يزيد النَّخَعي who said neither abu Bakr, nor 'Omar, nor 'Othman offered two rak'a before al-maghrib. He even called it bid'ah and also quoted sahaba who lived in al-Kufa like ibn Mas'ud, Hudaifah, 'Ammar and abu Su'ud who didn't offer it. Here the full statement from sharh (the comment of) ibn Battal of sahih al-Bukhari:
قال إبراهيم النخعى: لم يصل الركعتين قبل المغرب أبو بكر، ولا عمر، ولا عثمان، وقال إبراهيم: هما بدعة. قال: وكان خيار أصحاب رسول الله - صلى الله عليه وسلم - بالكوفة على، وابن مسعود، وحذيفة، وعمار، وأبو مسعود، فأخبرنى من رمقهم كلهم، فما رأى أحدًا منهم يصلى قبل المغرب،
Note that ibn Bital said that this was not only the view of abu Hanifa and Malik but also a-Shafi'i, but it is known that the shafi'i school affirms praying two rak'a before al-maghrib (al-Bayhaqi quoted a list of ahadith on both views in his sho'ab al-Imaan). So either ibn Battal was wrong or he only knew about a-Shafi'is early madhab, as the view of the shafi'i school is that one can offer two rak'a after the adhan of al-maghrib and before the iqamah, as at that time, the time for prohibition or when it is frowned upon to pray ends. One of their evidences is this hadith form sahih al-Bukahri and this one from sahih Muslim and our Messenger didn't hinder the sahaba from doing this according to this hadith.