Semantically mas and porem are equivalent, but porem is more flexible concerning the position in a sentence. Mas can only be used at the beginning of the subordinate clause, porem can stay as well after the subject of the subordinate clause. There can be a subtle difference in the meaning.
|mas is always at the beginning
|Ninguém respondeu à pergunta,
| os alunos sabiam a resposta.
|Nobody answered the question,
| the students knew the answer.
|porém can stay as well after the subject of the main clause
|Ninguém respondeu à pergunta; os alunos,
| sabiam a resposta.
|Nobody answered the question, the students
| knew the answer.
|Tentei chegar mais cedo, porém não consegui.
|I tried to come earlier, however I didn't made it in time.
|Podem ficar, porém este estabelecimento fecha às sete.
|You can stay, however the building closes at seven o'clock.
|Existem, porém alguns sinais encorajantes.
|There are however some encouraging signals.
|O comércio global não deve, porém ser desvirtuado.
|The global trade shoudn't be attracted.