The rules to apply in the use of essere and avere as auxiliary verbs are very complicated. See a summary below.
Transitive verbs (we simplify a bit) are conjugated with avere.
L' ho fatto <=> I have done it.
Intransitive verbs (we simplify a bit) are conjugated with essere.
È andato via <=> He has gone away.
If there is no reflexive pronoun and no modal verb (in the case that there is a modal verb there is an infinitive as well and the pronoun can be added to the infinitive) the auxiliary verb depends on the main verb. If the main verb requires essere, essere is to be used, otherwise avere.
L' ho potuto fare <=> I can have done it.
potuto andare via <=> He can have done it.
If there is a modal verb and a reflexive pronoun the reflexive pronoun can be put in front of the modal verb (Mi posso lavare) or can be added to the infinitive (Posso lavarmi). If it is put before the modal verb essere is compulsory and it doesn't matter which auxiliary the main verb requires normally. If the reflexive pronoun is added to the infinitive, the auxiliary verb required by the main verb is used. (Fermare is reflexive in Italian.)
Mi sono potuto fermare. <=> I can have stoped. Mi sono voluto comprare questo libro. <=> I wanted to buy the this book (for myself).
If there is a modal verb and the reflexive pronoun is added to the infinitive, the auxiliary verb to use depends on the auxiliary verb required by the infinitive.
Ho potuto fermarmi. <=> I can have stoped. Ho voluto comprarmi questo libro. <=> I wanted to buy this book (for myself).